I have a query that I just found in a database that does not report a crash. The main essence of the request:
Select * From table Where IsNull(myField, '') <> '' And IsNumeric(myField) = 1 And Convert(int, myField) Between @StartRange And @EndRange
Now myField does not contain numeric data in all rows [this is an nvarchar type] ... but this query was explicitly designed in such a way that it only cares about rows where the data in this field is numeric.
The problem is that T-SQL (as I understand it) does not close the Where clause, causing it to break out on records where the data is not numeric with the exception:
Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the nvarchar value '/A' to data type int.
With the exception of dumping all rows where myField is numeric to a temporary table and then asking that for rows where the field is in the specified range, what can I do is optimal?
My first parsing is simply trying to parse the returned data and see what happens:
Select * From ( Select * From table Where IsNull(myField, '') <> '' And IsNumeric(myField) = 1 ) t0 Where Convert(int, myField) Between @StartRange And @EndRange
But I get the same error as in the first request, which I am not sure I understand, since I am not converting data that should not be numeric at the moment. A subquery should only return rows in which myField contains numeric data.
Maybe I need my morning tea, but for whom does it make sense? Another set of eyes will help.
Thanks in advance
tsql short-circuiting sql-server-2000
Benalabaster
source share