Linear CASE Expressions
Element | Description | Restrictions | Syntax |
---|---|---|---|
expr | Expression that returns a value of some data type | Data type of expr that follows the WHEN keyword must be compatible with data type of the expression that follows the CASE keyword. Data type of expr in the THEN clause must be compatible with data types of expressions in other THEN clauses. | Expression |
The database server evaluates the expression that follows the CASE keyword, and then processes the WHEN clauses sequentially. If an expression after the WHEN keyword returns the same value as the expression that follows the CASE keyword, the database server uses the value of the expression that follows the THEN keyword as the overall result of the CASE expression. Then the database server stops processing the CASE expression.
If none of the WHEN expressions return the same value as the expression that follows the CASE keyword, the database server uses the expression of the ELSE clause as the overall result of the CASE expression (or, if no ELSE clause was specified, the returned value of the CASE expression is NULL).
SELECT title, CASE movie_type WHEN 1 THEN 'HORROR' WHEN 2 THEN 'COMEDY' WHEN 3 THEN 'ROMANCE' WHEN 4 THEN 'WESTERN' ELSE 'UNCLASSIFIED' END, our_cost FROM movie_titles;
In linear CASE expressions, the data types of WHEN clause expressions must be compatible with that of the expression that follows the CASE keyword.