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
| Parameter | Type | Required | Description |
|---|---|---|---|
when | ARRAY<BOOLEAN> | Yes | Array of boolean conditions to evaluate in order dynamic array |
then | ARRAY<T> | Yes | Array of values to return, corresponding to each when condition dynamic array |
else | T | No | Value 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_WHENwith parallel arrays). whenandthenarrays must have the same length- If no condition matches and
elseis not provided, returns NULL - Short-circuits: stops evaluating once a condition is true
- All
thenvalues andelsevalue 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 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT |
|---|---|---|---|
| 2 | 999 | 1 | null |
On this page