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 dynamic array
thenARRAY<T>YesArray of values to return, corresponding to each when condition dynamic array
elseTNoValue to return if no condition is true (optional)

Notes

  • The same logic is usually written in FeatureQL as CASE WHEN … THEN … [ELSE …] END (see the CASE WHEN operator page). This function is the registry encoding (CASE_WHEN with parallel arrays).
  • 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
    -- Second condition matches
    f1 := CASE_WHEN(ARRAY(FALSE, TRUE, FALSE), ARRAY(1, 2, 3), 999),
    -- No condition matches, returns else value
    f2 := CASE_WHEN(ARRAY(FALSE, FALSE), ARRAY(1, 2), 999),
    -- First condition matches (short-circuits)
    f3 := CASE_WHEN(ARRAY(TRUE, TRUE), ARRAY(1, 2), 999),
    -- No match, no else, returns NULL
    f4 := CASE_WHEN(ARRAY(FALSE, FALSE), ARRAY(1, 2))
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 BIGINT
29991null