I\'ve got a query that I\'ve just found in the database that is failing causing a report to fall over. The basic gist of the query:
Select *
From table
Wher
You'd have to force SQL to evaluate the expressions in a certain order. Here is one solution
Select *
From ( TOP 2000000000
Select *
From table
Where IsNumeric(myField) = 1
And IsNull(myField, '') <> ''
ORDER BY Key
) t0
Where Convert(int, myField) Between @StartRange And @EndRange
and another
Select *
From table
Where
CASE
WHEN IsNumeric(myField) = 1 And IsNull(myField, '') <> ''
THEN Convert(int, myField) ELSE @StartRange-1
END Between @StartRange And @EndRange
SQL is declarative: you tell the optimiser what you want, not how to do it. The tricks above force things to be done in a certain order.