Help with CASE statement.

  • 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

  • You can do

    case

    when x = 'A' or x = 'b' then x

    ...

  • Thanks Steve!

  • Case

    When x in ('a','b','c') then 'yep'

    Else 'nope'

    End



    PeteK
    I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.

  • Thanks a lot

  • 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