IFNULL: from Tableau to SQL

Replaces a null value with a fallback. Below, 8 sample rows go through the calculation and through the SQL, and both return the same numbers.

Calculated field

IFNULL([amount], 0)

Sample data

labelquantityamountflag
cedar cedar 6512.8True
ORCHID-west66-7.6False
maple-east670null
willow north6825.3True
birch-birch694.9False
pine/south70-1.2null
ash719True
nullnullnullnull

The same calculation as SQL

SELECT coalesce(amount,0) AS result FROM input

Runs the example above.

Result

result
12.8
-7.6
0
25.3
4.9
-1.2
9
0

Same rows from the calculation and from the SQL in DuckDB.

Where a naive translation goes wrong

  • Replacing IFNULL with amount changes the shown values or null handling.

    Naive SQL

    SELECT amount AS result FROM input
    result
    12.8
    -7.6
    0
    25.3
    4.9
    -1.2
    9
    null

    Matches the calculation

    SELECT coalesce(amount,0) AS result FROM input
    result
    12.8
    -7.6
    0
    25.3
    4.9
    -1.2
    9
    0

Signature

IFNULL(expression, fallback)