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 ] ENDNotes
- Write this as
CASE WHEN … THEN … [ELSE …] ENDin FeatureQL (same shape as familiar SQL). Conditions are evaluated in order; the first one that is TRUE selects itsTHENvalue. - If no
WHENis TRUE andELSEis omitted, the result is NULL (typed with the common type of the branch values—useNULL::<type>or typedNULL(...)where inference needs a type). - Evaluation short-circuits: once a condition is TRUE, later branches are not evaluated.
- Every
THENexpression and theELSEexpression (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 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT |
|---|---|---|---|
| 2 | 999 | 1 | null |
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 VARCHAR | f2 VARCHAR | f3 BIGINT |
|---|---|---|
| yes | big | 42 |