CASE_WHEN_VALUE()
All functions > CONDITIONAL > CASE_WHEN_VALUE()
Returns the result corresponding to the first matching value in a simple case expression
Signatures
Returns: the value corresponding to the first match, or else value if none match
CASE_WHEN_VALUE(when_base: T, values_to_match: ARRAY<T>, then: ARRAY<U>, else: U) → U sql
| Parameter | Type | Required | Description |
|---|---|---|---|
when_base | T | Yes | Expression to compare against values |
values_to_match | ARRAY<T> | Yes | Array of values to match against when_base |
then | ARRAY<U> | Yes | Array of values to return, corresponding to each match value |
else | U | Yes | Value to return if no value matches |
Notes
- The same logic is usually written as
CASE <expr> WHEN … THEN … [ELSE …] END(see the CASE WHEN VALUE operator page). This function is the registry encoding. - Compares
when_baseagainst each value invalues_to_matchusing equality values_to_matchandthenarrays must have the same length- All
thenvalues andelsevalue must be of compatible types
Examples
FeatureQL
SELECT
f1 := CASE_WHEN_VALUE(2, ARRAY[1, 2, 3], ARRAY['a', 'b', 'c'], 'z'), -- Value 2 matches position 2, returns 'b'
f2 := CASE_WHEN_VALUE(5, ARRAY[1, 2, 3], ARRAY['a', 'b', 'c'], 'z'), -- Value 5 not in array, returns else value
f3 := CASE_WHEN_VALUE('cat', ARRAY['dog', 'cat', 'bird'], ARRAY[1, 2, 3], 999) -- String matching
;Result
| f1 VARCHAR | f2 VARCHAR | f3 BIGINT |
|---|---|---|
| b | z | 2 |
On this page