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 FALSE, NULL if condition is 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
- Condition must evaluate to BOOLEAN
- true_value and false_value must be of compatible types
- If condition is NULL, returns NULL
- If false_value is omitted, returns NULL when condition is FALSE
- Short-circuits evaluation: only evaluates the branch that will be returned
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 returns NULL
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 | null | yes | no | 100 | null |
On this page