Case Stmt with multiple "when" params and one "then" stmt

  • Is it possible to have multiple "when" parameters for one "then" statement? For example:

    Case ActionType

    When 'TMA' or 'TMB' or 'TMC'

    Then 12345

    OR

    Case ActionType

    When 'TM%'

    Then 12345

    I know I can do it with multiple statements:

    Case ActionType

    When 'TMA'

    Then 12345

    When 'TMB'

    Then 12345

    When 'TMC'

    Then 12345

    but that will be very unweildly.

    Any thoughts? Thanks

  • You can do:

    Select

    Case

    When ActionType in ('TMA','TMB','TMC') Then 12345

    When ActionType in ('TMD','TME','TMF') Then 54321

    Else 0

    End

  • Thanks, you are a life saver. Thanks for helping a nube struggling with simple things.

    Your solution worked great!

  • Ummm... rather than struggling with syntax, do you know what Books Online is? Comes free with SQL Server and, although it sometimes takes a bit to find what you're looking for, has great examples for things like CASE...

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I will take a look - thanks.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply