I've been following a few discussions on case statement processing order lately. Sometimes things do not work as you expect. A simple example:select case when max(n) = 1 then 1 when max(1/0) = 1 then 2 endfrom (values (1)) v(n)
results in:Msg 8134, Level 16, State 1, Line 1Divide by zero error encountered.
even though the first "when" is satisfied. This is because aggregations are evaluated first. (modify the sample above to remove the "max"). It should workWhat I'm wondering about now is, can we count on the processing order of the When clauses themselves (not considering aggregates). e.g. consider this query:SELECT title, price, Budget = CASE price WHEN price > 20.00 THEN 'Expensive' WHEN price BETWEEN 15.00 AND 25.99 THEN 'Moderate' WHEN price < 10.00 THEN 'Inexpensive' ELSE 'Unknown' END,FROM titles
Now, suppose that price = 21.00. Are there any circumstances under with this query returns 'Moderate'. If not, can I safely simplify this query to:SELECT title, price, Budget = CASE price WHEN price > 20.00 THEN 'Expensive' WHEN price < 10.00 'Inexpensive' -- Can't be more than 19.99 ELSE 'Moderate' -- must be between 10 and 19.99 END,FROM titles