CASE Clause

Conditional processing evaluates set conditions to determine a result.

Syntax

CASE 
       WHEN condition THEN expression [...] ELSE expression
END

Components

condition

An expression that evaluates to either zero or non-zero. A non-zero result indicates the condition is true, a result of zero indicates the condition is false.

expression

The result of the evaluated conditions. This can be any valid expression or variable.

Usage

A CASE clause is order-dependent and contains conditional expressions that require the parameters WHEN, THEN, and ELSE. WHEN conditions filter the specific case and narrow down the result through evaluations of whether the conditions set are true or false. If true, following THEN expressions are carried out. If false, subsequent WHEN conditions are tested.

If all conditions prior to the ELSE parameter are false, then the ELSE expression is executed. The CASE clause closes with the keyword END.

Example

This example filters weights and specifies a number to each condition set.

CASE
WHEN weight<500 THEN 1
WHEN weight>1000 THEN 3
ELSE 2
END