I am getting the following error:
Conversion failed when converting the varchar value '2010-01-10' to data type int.
While running the following command in a query window in Management Studio:
SELECT * FROM (
SELECT CAST(group_id AS INT) AS qqq from view_jct_snapshot_group_items
) AS X
WHERE qqq = CAST(14290 AS INT)
However, when I run just this portion of the query, I get results and no errors:
SELECT CAST(group_id AS INT) AS qqq from view_jct_snapshot_group_items
Similarly with the following:
SELECT * FROM (
SELECT CAST(group_id AS INT) AS qqq from view_jct_snapshot_group_items
) AS X
What is going on, and how can I use my where clause without getting an error?
The table that is the source data for the view most likely has the value
'2010-01-10'in thegroup_idcolumn and this is excluded by a filter in the View itself.Views are inline constructs expanded out by the query optimiser and it looks like your query is pushing the cast down to before the filter occurs.
Try