February 18, 2008 at 8:50 am
Any way to check multiple conditions in one WHEN using a CASE statement e.g.
CASE somefiled
WHEN 'a' 'b' 'c' THEN 'X'
WHEN 'e' 'f' 'g' THEN 'Y'
ELSE 'Z'
END
Basically trying to avoid having multiple WHENs for a given THEN.
Thanks
February 18, 2008 at 9:05 am
You can do
case
when x = 'A' or x = 'b' then x
...
February 18, 2008 at 9:20 am
Thanks Steve!
February 19, 2008 at 8:40 am
Case
When x in ('a','b','c') then 'yep'
Else 'nope'
End
June 14, 2009 at 6:49 am
Thanks a lot
June 18, 2009 at 10:32 am
Might be beating a dead horse, but be sure you are evaluating your conditions in the right order. Best answer below is '@x=1 AND @z=3', but that is evaluated after '@x=2 OR @z=3' and therefore is not the selected answer.
Had this come up in a more subtle form lately, but can't find the dang thing, so fudging it. 😉
DECLARE @x int, @y int, @z int
SET @x = 1
SET @y = 2
SET @z = 3
SELECT
CASE
WHEN @x=2 or @z=3 THEN '@x=2 OR @z=3'
WHEN @x=1 AND @z=3 THEN '@x=1 AND @z=3'
ELSE '@y=2'
END
---------------------------------------------------------
How best to post your question[/url]
How to post performance problems[/url]
Tally Table:What it is and how it replaces a loop[/url]
"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply