Skip to main content
Compares two expressions. Returns <expression1> if it’s non-NULL, otherwise returns <expression2>.

Syntax

IFNULL(<expression1>, <expression2>)

Parameters

ParameterDescriptionSupported input types
<expression1>, <expression2>Expressions that evaluate to any data type that Firebolt supports.Any

Return Types

Same as input type

Remarks

Use ZEROIFNULL(<expression>) as a synonym shorthand for IFNULL(<expression>, 0). IFNULL(a, b) has the same behaviour as COALESCE(a, b).

Examples

The following example shows the core behavior: IFNULL returns the original value when it is non-NULL, and returns the replacement when it is NULL:
SELECT val, IFNULL(val, 0) AS val_or_zero
FROM UNNEST(ARRAY[1, NULL, 3, NULL, 5]) AS t(val)
ORDER BY val NULLS LAST;
val int nullval_or_zero int
11
33
55
NULL0
NULL0

Rows: 5Execution time: 4.15ms

The following example uses IFNULL to replace the nextlevel value with 0 for the final level in the levels table, which has no next level (NULL):
SELECT level, name, nextlevel, IFNULL(nextlevel, 0) AS next_level_or_zero
FROM levels
ORDER BY level;
level int nullname text nullnextlevel int nullnext_level_or_zero int
1Thunderbolt Circuit22
2Velocity Vale33
3Raceway Ridge44
4Nitro Narrows55
5Thunder Road66
6Burnout Boulevard77
7Speed Street88
8Racing Ravine99
9Drift District1010
10Acceleration AlleyNULL0

Rows: 10Execution time: 6.00ms