Thursday, October 3, 2024

How to replace NULL with Empty String in SQL Server? ISNULL() vs COALESCE() Examples

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 like 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 databases 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.

For SQL Server and T-SQL beginners, I also recommend Microsoft SQL for Beginners online course by Brewster Knowlton on Udemy. It''s a great course to learn T-SQL concepts, functional, and SQL Server basics.




Replacing NULL with blank in SQL SERVER - ISNULL() Example

Let's first see, how to use ISNULL() to replace NULL String to empty String in SQL SERVER. In order to understand the problem and solution better, let's create a sample database with some values.


IF OBJECT_ID( 'tempdb..#People' ) IS NOT NULL
DROP TABLE #People;

CREATE TABLE #People (first_name varchar(30), last_name varchar(30));

INSERT INTO #People VALUES ('Joe','Root');
INSERT INTO #People VALUES ('Mary', NULL);
INSERT INTO #People VALUES (NULL, 'Broad');

-- cleanup
-- DROP TABLE #People

Now let's display the first name, last name and full name from #People table, where the full name is nothing but a concatenation of first and last name. Here is our SQL query:

SELECT first_name, last_name, first_name + last_name AS full_name FROM #People
first_name last_name full_name
Joe        Root      JoeRoot
Mary       NULL      NULL
NULL       Broad     NULL

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:

SELECT first_name, last_name, ISNULL(first_name,'') + ISNULL(last_name,'') 
as full_name FROM #People
first_name last_name full_name
Joe        Root      JoeRoot
Mary       NULL      Mary
NULL       Broad     Broad

You can see that even though one of the joining columns 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 the following SQL query:

SELECT first_name, last_name, COALESCE(first_name,'') + COALESCE(last_name,'')
 as full_name FROM #People
first_name last_name full_name
Joe        Root      JoeRoot
Mary       NULL      Mary
NULL       Broad     Broad


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 the 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 the value of first_name if last_name is NULL in the report. Following SQL query uses COALESCE to do that:

SELECT 
COALESCE(first_name,last_name, '') as first_name,
COALESCE(last_name, first_name,'') as last_name,
COALESCE(first_name,'') + COALESCE(last_name,'') as full_name
FROM #People
first_name last_name full_name
Joe        Root      JoeRoot
Mary       Mary      Mary
Broad      Broad     Broad

Here is the screenshot of SQL queries from Microsoft SQL SERVER 2014 database to give you full view:


How to replace NULL with empty String in SQL SERVER




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 functions 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 like Oracle, MySQL, and PostgreSQL.


Further Learning
Introduction to SQL
The Complete SQL Bootcamp
SQL for Newbs: Data Analysis for Beginners

2 comments :

Mukesh Singh said...

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

Anonymous said...

technically, ISNULL is ever-so-slightly faster than COALESCE for two sources (which is also to say that for three sources, ISNULL(ISNULL(x)) is slower than COALESCE). Singleton lookups won't matter as they'll lose more time to seek latency than the CPU difference, but it can make a difference during bulk transfers.

Post a Comment