CASE WHEN

All functions > CONDITIONAL > CASE WHEN

Returns the result of the first condition that evaluates to true

Syntax

CASE WHEN condition THEN result { WHEN condition THEN result ... } [ ELSE default ] END

Notes

  • Write this as CASE WHEN … THEN … [ELSE …] END in FeatureQL (same shape as familiar SQL). Conditions are evaluated in order; the first one that is TRUE selects its THEN value.
  • If no WHEN is TRUE and ELSE is omitted, the result is NULL (typed with the common type of the branch values—use NULL::<type> or typed NULL(...) where inference needs a type).
  • Evaluation short-circuits: once a condition is TRUE, later branches are not evaluated.
  • Every THEN expression and the ELSE expression (if present) must be type-compatible.

Related Functions

Examples

Searched CASE

FeatureQL
SELECT
    f1 := CASE WHEN FALSE THEN 1 WHEN TRUE THEN 2 WHEN FALSE THEN 3 ELSE 999 END, -- Second WHEN is first true branch
    f2 := CASE WHEN FALSE THEN 1 WHEN FALSE THEN 2 ELSE 999 END, -- No WHEN true, ELSE applies
    f3 := CASE WHEN TRUE THEN 1 WHEN TRUE THEN 2 ELSE 999 END, -- First true branch short-circuits
    f4 := CASE WHEN FALSE THEN 1 WHEN FALSE THEN 2 END -- No match and no ELSE → NULL
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 BIGINT
29991null

String results

FeatureQL
SELECT
    f1 := CASE WHEN 1 = 1 THEN 'yes' ELSE 'no' END, -- Expression in WHEN
    f2 := CASE WHEN 10 > 5 THEN 'big' WHEN 10 > 20 THEN 'huge' ELSE 'small' END, -- First matching inequality
    f3 := CASE WHEN TRUE THEN 42 ELSE 0 END -- Single WHEN with ELSE
;
Result
f1 VARCHARf2 VARCHARf3 BIGINT
yesbig42

Last update at: 2026/05/26 17:22:09