CASE_WHEN

All functions > CONDITIONAL > CASE_WHEN

Returns the result of the first condition that evaluates to true

Signatures

Returns: the value corresponding to the first true condition, or else value if none match

CASE_WHEN(when: ARRAY<BOOLEAN>, then: ARRAY<T>, [else: T]) → T
sql
ParameterTypeRequiredDescription
whenARRAY<BOOLEAN>YesArray of boolean conditions to evaluate in order
thenARRAY<T>YesArray of values to return, corresponding to each when condition
elseTNoValue to return if no condition is true (optional)

Notes

  • Evaluates conditions in order and returns the first matching result
  • when and then arrays must have the same length
  • If no condition matches and else is not provided, returns NULL
  • Short-circuits: stops evaluating once a condition is true
  • All then values and else value must be of compatible types

Examples

FeatureQL
SELECT
    f1 := CASE_WHEN(ARRAY[FALSE, TRUE, FALSE], ARRAY[1, 2, 3], 999),  -- Second condition matches
    f2 := CASE_WHEN(ARRAY[FALSE, FALSE], ARRAY[1, 2], 999),  -- No condition matches, returns else value
    f3 := CASE_WHEN(ARRAY[TRUE, TRUE], ARRAY[1, 2], 999),  -- First condition matches (short-circuits)
    f4 := CASE_WHEN(ARRAY[FALSE, FALSE], ARRAY[1, 2])  -- No match, no else, returns NULL
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 BIGINT
29991null

Last update at: 2026/03/03 16:47:38
Last updated: 2026-03-03 16:48:19