CASE_WHEN
All functions > CONDITIONAL > CASE_WHEN
Returns the result of the first condition that evaluates to true
Syntax
CASE_WHEN(when: ARRAYBOOLEAN, then: ARRAY<T>, else: T) → T
sql
Type Parameters:
T
: Any expression type
Arguments
Parameter | Type | Required | Description |
---|---|---|---|
when | ARRAYBOOLEAN | 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) |
Returns
Same type as then values - returns the value corresponding to the first true condition, or else value if none match
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