Monday, April 11, 2011

TSQL - How to use a case statement for 2 columns?

I want to do a case on the result of 2 columns. How do I do this?

e.g.:

SELECT CASE amount=100 AND DATE IS NOT NULL WHEN 0 THEN 'Something' ELSE ''

Something like that?

From stackoverflow
  • select case
        when amount = 100 and date is not null then 'something'
        else 'something else'
        end
    

    This is a "searched case expression" (see MSDN):

        CASE
          WHEN Boolean_expression THEN result_expression [ ...n ] 
          [ ELSE else_result_expression ]  END
    
    Mitch Wheat : beat me by 2 seconds!
  • select someColumnName,
           case 
             when amount = 100  AND someothercondition then 'XXX'
             when amount = 1000  AND anothercondition then 'YYY'
             else 'WWW' 
           end as "MyColumnName"
    from myTable
    
  • select case when amount = 100 and date is not null then '0' else 'something else' end

  • Look for SELECT WHEN

    http://www.4guysfromrolla.com/webtech/102704-1.shtml

0 comments:

Post a Comment