What is the SQL case statement ? (or switch)

About

In sql, the case expression 1) is a conditional expression that is the equivalent to the more general switch expression (same function, just another word).

Example

As for a switch statement, there is also two ways to write a case statement:

  • with values
  • with conditional expressions

Values based Case

CASE  Promotions."Promo Subcategory"
WHEN  'TV commercial'          then 'Commercial'
WHEN  'TV program sponsorship' then 'Sponsor'
ELSE  'Default'
END

Conditional Expression based Case

CASE WHEN "Sales Facts"."Amount Sold" > 600000  THEN 'Big'
     WHEN "Sales Facts"."Amount Sold" > 300000  THEN 'Middle'
     ELSE 'Small' END

Powered by ComboStrap