This one has us perplexed ...
We have a query that uses CAST to convert a float to a decimal, this query joins a number of tables to find the rows to return. One of the rows in one of the tables contains a value that when CAST to a decimal causes an Arithmetic Overflow Error.
The strange thing is that the row that has this value is NOT one of the rows that is being returned in the result set.
Overly simplified example:
ID Value
1 1.1
2 11.1
3 11111.1
Query:
SELECT Id, CAST(value as decimal(4,1))
FROM <complex number of joins>
WHERE <conditions that don't return row with Id 3>
... Arithmetic Error
If we explicitly exclude that row in the WHERE clause then the error goes away. Eg. WHERE ... AND Id <> 3
.. Works fine
Does anyone know how this is possible?
NOTE: The issue here is not that the CAST fails on row with Id 3! The issue is that the WHERE clause excludes the row with Id 3, and yet the query still fails. How can the query fail if the row with value 11111.1 is not being returned by the WHERE clause?