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 |
then | ARRAY<T> | Yes | Array of values to return, corresponding to each when condition |
else | T | No | Value 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 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT |
|---|---|---|---|
| 2 | 999 | 1 | null |
On this page