If you have NULL values in your column, you won’t be able to perform functions on it or combine it with another column. NULL results will be ignored and excluded from your results. This is a result of what’s called three-value logic (3VL) in SQL. Please refer to the following page for a detailed explanation.
Replacing NULL values with zero using coalesce
If you’d like to replace all NULL values in a column with another value, the
coalesce function easily lets you do this. This function accepts two or more arguments and returns the first non-NULL argument; the function must have at least 2 arguments.
The basic format looks like this:
coalesce("column", replace_value, ...)
column: The column containing the NULL values you want to replace with the specified
replace_value: The column or values you want to replace your NULL values with. You can have more than one
To replace NULL values with the
coalesce() in the Data Pipeline, click +Add Transformation and choose Add Column from the menu options. To replace any column name that could contain NULL values with
0, use the following formula (replacing
Column Name with the name of your column):
coalesce("Column Name", 0)
You’ll likely want to hide your original column by adding a Hide Columns step after the Add Column step in the Data Pipeline.
Tip! To minimize the number of Pipeline steps, you can combine multiple functions in a single formula, as shown in the screenshot above.
Replacing NULL and blank values with a CASE statement
coalesce() doesn’t fully replace all empty values in your query results, your result set may contain both NULL and blank values. To account for both cases, use a CASE statement instead of
To do this, you can either use an Add Column or an Edit Column Pipeline step and follow these steps:
- Select the column to which you want to add zero fill.
- Select Custom formula as the Formula Type.
- Enter the following case statement in the Formula field, replacing
Column Namewith your column’s name:
case when "Column Name" is null then 0 when "Column Name" = '' then 0 else "Column Name" end
Replacing NULL values using another column
You can also use
coalesce() to replace NULL values found in one column with another column’s values. This would be carried out at the row level. For example, if column A’s value is NULL in row 3 and you want to replace that NULL value with a value from column B, the function would replace the NULL from column A with the value from column B in row 3.
coalesce("Column A", "Column B", ...)
Check out our step-by-step video showing how to do this: