Thursday, October 26, 2017

We often need to replace NULL values with empty String or blank in SQL e.g. while concatenating String. In SQL Server, when you concatenate a NULL String with another non-null String the result is NULL, which means you lose the information you already have. To prevent this, you can replace NULL with empty String while concatenating. There are two ways to replace NULL with blank values in SQL Server, function ISNULL() and COALESCE(). Both functions replace the value you provide when the argument is NULL e.g. ISNULL(column, '') will return empty String if the column value is NULL. Similarly, COALESCE(column, '') will also return blank if the column is NULL.
The only difference between them is that ISNULL() is Microsoft SQL Server specific but COALESCE() is the standard way and supported by all major database like MySQL, Oracle and PostgreSQL. Another difference between them is that you can provide multiple optional values to COALESCE() e.g. COALESCE(column, column2, ''), so if the column is null then it will use column2 and if that is also null then it will use empty String.

You can see that full_name is NULL for the second and third record because for them either first_name or last_name is NULL. In order to avoid that and to replace the NULL with empty String, let's use ISNULL() method in our SQL query:

You can see that even though one of the joining column is NULL but full_name is not NULL anymore because ISNULL() is replacing NULL values with a blank.

Using COALESCE() to replace NULL with empty String in SQL SERVER

In the earlier example, you have learned how to use ISNULL() to replace NULL values with blank in SQL SERVER, let's see how can we use COALESCE() to do the same. Remember, COALESCE() is a standard function and whenever you can use COALESCE() you should be using it. In this example, you don't need to do anything, just replace ISNULL() with COALESCE() and you are done, as shown in following SQL query:

Let me show you another use of COALESCE() function while we are using it. You can use COALESCE() to instruct using the value of another column if the target column is NULL and if that is also null then use third column and so on. You can use this technique to provide sophisticated default values in your reports. For example, in this scenario, let's display the value of last_name if first_name is NULL and value of first_name if last_name is NULL in the report. Following SQL query uses COALESCE to do that:

That's all about how to replace NULL with empty String or blank in SQL SERVER. You can use ISNULL() or COALESCE() to replace NULL with blanks. It's particularly important to use these function while concatenating String in SQL SERVER because one NULL can turn all information into NULL.

Btw, you can also use CONCAT() instead of + operator to avoid NULL, this function returns the value of nonnull argument if another argument is NULL. Between ISNULL() and COALESCE(), use ISNULL() if you know for sure that your code will run on Microsoft SQL Server but COALESCE() is better because it's standard and you can use it to replace NULL with empty String in any database e.g. Oracle, MySQL and PostgreSQL.

1 comment
:

Thanks for sharing the information. Generally, it is recommended to stick to standard features unless there’s some flexibility or performance advantage in the non-standard feature that is a higher priority. ISNULL is actually more limited than COALESCE, so generally, it is recommended to stick to COALESCE.SQL - ISNull vs COALESCE functions