Here is my sql
SELECT * FROM Answers
WHERE QuesID = 1 AND OptionID IN (2,5) AND CONVERT(float, ISNULL(AnswerValue, 0)) < 300
The problem is there is legacy data that is string in OptionID = 6. When I run the above query it gives me
Error converting data type varchar to float.
I confirmed all data in QuesID=1 and OptionId IN (2,5) is all float data. It seems like SQL is doing the math part of this sql first, is there anyway to make it check the OptionID before running the AnswerValue comparison? Or am I completely off base?
You can’t really control the order in which SQL will “resolve” the clauses in your
WHEREclause. I did some quick testing, and if there was a string value entered only for QuesId=3, it worked.So, yes, as you posted while I was writing this, tossing in
will handle this, as SQL appears to evaluate it earlier in its process.