Linear CASE Expressions

A linear CASE expression compares the value of the expression that follows the CASE keyword with an expression in a WHEN clause.
Linear CASE Expression

1  CASE expr +  WHEN expr THEN
2.1 expr
2.1 NULL?  ELSE
2.1 expr
2.1 NULL  END
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).

The next example shows a linear CASE expression in the projection list of the Projection clause of a SELECT statement. For each movie in a table of movie titles, the query returns the title, the cost, and the type of the movie. The statement uses a CASE expression to derive the type of each movie:
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.