IF()
All functions > CONDITIONAL > IF()
Returns one value if a condition is true, another if false
Signatures
Returns: true_value if condition is TRUE, false_value if condition is FALSE or NULL
IF(condition: BOOLEAN, true_value: T, [false_value: T]) → T sql
| Parameter | Type | Required | Description |
|---|---|---|---|
condition | BOOLEAN | Yes | Boolean expression to evaluate |
true_value | T | Yes | Value to return if condition is TRUE |
false_value | T | No | Value to return if condition is FALSE (optional, defaults to NULL) |
Notes
- The condition must be
BOOLEAN(useNULL::BOOLEANwhen you need a typed unknown condition). true_valueandfalse_valuemust be type-compatible.- If the condition is NULL, it falls through to the else branch (returns
false_value, or NULL if omitted). This matchesCASE WHENbehavior. - If
false_valueis omitted, the result is NULL when the condition is FALSE or NULL. - Short-circuits: only the selected branch is evaluated.
Examples
FeatureQL
SELECT
f1 := IF(TRUE, 1, 2), -- Condition is TRUE, returns first value
f2 := IF(FALSE, 1, 2), -- Condition is FALSE, returns second value
f3 := IF(NULL::BOOLEAN, 1, 2), -- NULL condition falls through to else branch
f4 := IF(5 > 3, 'yes', 'no'), -- Expression evaluation TRUE
f5 := IF(5 < 3, 'yes', 'no'), -- Expression evaluation FALSE
f6 := IF(TRUE, 100), -- Omit false_value, Returns TRUE_value when TRUE
f7 := IF(FALSE, 100) -- Omit false_value, returns NULL when FALSE
;Result
| f1 BIGINT | f2 BIGINT | f3 BIGINT | f4 VARCHAR | f5 VARCHAR | f6 BIGINT | f7 BIGINT |
|---|---|---|---|---|---|---|
| 1 | 2 | 2 | yes | no | 100 | null |
On this page