I am having a time discovering how to best write the following:
SET @SAMPLE = (SELECT CASE
WHEN @A < 0.01 AND @B < 0.01 THEN -1
WHEN @A < 0.01 THEN @B
ELSE @C
END )
I do not get what I expect here. I am finding that @SAMPLE contains 0.00 after running this. Thanks for any guidance.
The CASE statement evaluates its conditions sequentially and stops with the first condition whose condition is satisfied
From your example, all one can deduct is that @B or @c is zero.
Operational precedence usually refers to which operator is first evaluated ("*" or "-", for example). Your question should probably be titled "Case evaluation order".
http://technet.microsoft.com/en-us/library/ms181765.aspx
Without values I cannot be sure that this is what you mean but it seems that you are asking in what order does a case statement evaluate its WHEN clauses. If that indeed is the question then the answer is fairly simple. A CASE WHEN will return the value of the THEN statement for the first WHEN that is true and will stop evaluating immediately once it returns. This means that in your example @Sample will evaluate first WHEN to last WHEN (and within the WHEN it evaluates left to right) such that your logical checks are:
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With