The query below...
SELECT YEAR(TRY_CAST(m.MetaValue AS DATE))
FROM MetaData m
...results in this error:
String or binary data would be truncated.
If I filter the query to a valid recordset, the error goes away:
SELECT YEAR(TRY_CAST(m.MetaValue AS DATE))
FROM MetaData m
WHERE m.MetaKey IN (...)
If I try to filter based on the selected expression, the query fails again:
SELECT * FROM (
SELECT YEAR(TRY_CAST(m.MetaValue AS DATE)) AS Yr
FROM MetaData m
WHERE MetaKey IN (...)
) x
WHERE x.Yr > 1900
What's going on here? I get the sense that SQL Server is optimizing the query into something along these lines, but I don't know how to prevent it:
SELECT YEAR(TRY_CAST(m.MetaValue AS DATE)) AS Yr
FROM MetaData m
WHERE MetaKey IN (...)
AND YEAR(TRY_CAST(m.MetaValue AS DATE)) > 1900