Skip to main content
CASE is a conditional expression. It returns the result in the THEN clause if a condition in the WHEN clause evaluates to true. If none of the conditions evaluate to true, it returns the result in the optional ELSE clause. If ELSE is not present, NULL is returned.

Syntax

The CASE expression comes in two forms:
  • Simple CASE
For this syntax, expression is compared to each expression in the WHEN clause. If an equal expression is found, the result in the THEN clause is returned. If no equal expression is found, the result in the ELSE clause is returned if ELSE is present.
  • Searched CASE
For this syntax, each condition in the WHEN clause is evaluated until one is true and the corresponding result in the THEN clause is returned. If no condition evaluates to true, the result in the ELSE clause is returned, if ELSE is present. The first CASE equals the second one as follows:

Parameters

  • expressionN: the expression to compare. Multiple expressions must be compatible in data types.
  • conditionN: the condition that can evaluate to a BOOLEAN value.
  • resultN must be compatible in data types.

Return value

The return value is of the common type of all types in the THEN clause.

Examples

Suppose table test_case has the following data:

Use simple CASE

  • ELSE is specified and the result in ELSE is returned if no equal expression is found.
  • ELSE is not specified and NULL is returned if no condition evaluates to true.

Use searched CASE with no ELSE specified

Keywords

case when, case, case_when, case…when