TSQL and case when with multiple whens?
Question
I have several conditions and the result for those should be the same. I searched the net and found stuff like this:
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END
That's nice, but not what I need, for me its more like R,M,T and S all have the same result and A,B,C,D for example don't have. How would I do this? I cant connect with OR, or at least I did not manage to :). Something like this maybe?
CASE ProductLine
WHEN 'R' OR 'M' OR ... THEN 'Road'
ELSE 'Not for sale'
END
Solution
Change to a "searched" CASE expression. You have a "simple" CASE expression above
CASE
WHEN ProductLine IN ('R', 'M', ...) THEN 'Road'
ELSE 'Not for sale'
END
From the MSDN link above:
Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow