Dealing with NULLs in SQL Server

What is NULL? NULL or the NULL marker is the way we represent an unknown value in SQL, by SQL I am referring to the standard Structured Query Language, not MS SQL Server. The last sentence takes us a little back to 1969 when the standard was first defined by Dr. E.F. Codd. NULL becomes necessary because we need to think in terms of what is called three-valued predicate logic. A predicate is the property of an expression that either holds or does not hold. At face value, one would think of two possibilities: TRUE or FALSE. However, there is a third possibility: UNKNOWN.

Let’s take an example. Assuming an attribute (column) in our relation (Table) represents Tax Identification Number (TIN) of a set of Small Business owners in Accra, Ghana. The column for each business owner’s record will be populated with his/her TIN and we can use this column to determine some other attribute such as whether he/she is up to date in Tax payments. However, there is are two extra possibilities in this use case:

  1. The Business Owner has a TIN and is up to date.
  2. The Business owner does not have a TIN and is (obviously) not up to date.

The above describes what Dr. Codd called four-valued predicate logic. The SQL standard, however, simplifies these two additional conditions by defining it as unknown i.e. NULL. We do not know the TIN of the business owner and cannot determine any other value from the attribute of the affected Business Owner. So, NULL is UNKNOWN and is the third value in standard three-valued predicate logic.

NULL is Special

The definition of NULL necessitates the treatment of the marker in a different way from actual values. The following are examples:

  1. There is no such thing as the filter “WHERE = NULL;”. The correct expression would “WHERE IS NULL;”. The same goes for the converse expression.
  2. When sorting in ascending order SQL you can choose to list NULLs first or last. The default is to list NULLs first.
  3. You cannot compare NULL values. This should be obvious since we have said that NULL is UNKNOWN.
  4. When a concatenation attempt involves a NULL column, the result is NULL.

Common NULL-Related Functions

The following are three common NULL related functions in SQL Server

ISNULL

ISNULL – Replaces NULL with a specified replacement value. Listing 1 and Fig 1 show simple examples of ISNULL.

Fig 1: Simple Example of ISSNULL
Fig 1: Simple Example of ISNULL

NULLIF

NULLIF returns NULL is the value of the two arguments are equal.

Fig. 2: Simple Example of NULLIF

COALESCE

COALESCE returns the first non-NULL value from the list provided. Listing 1 shows examples of this and Fig 1 shows the output of the queries.

Fig 3: Simple Example of Coalesce

Notice that these simple examples expose to use the nature of NULL. NULL and blank space is NOT the same. In the first statement, COALESCE returns a blank space showing us that a blank space is the first non-NULL value in the list.

Differences between ISNULL and COALESCE

The differences between ISNULL and COALESCE has been the subject of several online articles as well as books such as those listed in the references section. These differences are summarized as follows:

  1. ISNULL is proprietary to SQL Server while COALESCE is an ANSI standard function. This implies that for portability, COALESCE is preferred.
  2. ISNULL takes only two arguments while COALESCE can take more than two arguments.
  3. The data type of the value returned by ISNULL is determined by the data type of the first argument while the data type of the value returned by COALESCE is determined by the data type in the list with the highest precedence.
  4. When both functions are used with subqueries, ISNULL performs better because COALESCE is internally translated to a CASE expression making it tend to repeat scans.

Itzik Ben-Gan also explores other implications of the conversion of COALESCE to a case expression in his article the link to which is provided at the end of this article.

Sample Use Cases

We want to display on a web portal the list of customers based on certain criteria from the table we are creating in Listing 4. Tasks 1 and 2 give to possible requirements and we use ISNULL and COALESCE to meet the requirements.

Fig. 4 Sample Table

Task 1: Return the List of all customers who did not provide an alternate phone number.

Using COALESCE (or ISNULL), we can present the information required much better using a text that says ‘No Secondary Phone’.

Fig. 5 Result Set for Listing 5

Task 2: Return the List of all customers, their primary Phone Number, and any other alternate phone number.

Fig. 6 Result Set for Listing 6

In this case, ISNULL is not an option since we are passing three arguments.

Conclusion

In this article, we have discussed the concept of NULL as it relates to three-valued predicate logic and described popular functions we use in SQL Server to deal with data sets containing NULLs. We have also seen examples of how these functions can be used. Many other references delve deeper into the use, benefits, and limitations of these functions. I would highly recommend Itzik Ben-Gan’s books and blogs as good sources of information.

References

Kenneth Igiri

Kenneth Igiri is a Database Administrator with eProcess International S.A., Ecobank Group's Shared Services Centre. Kenneth has over eight years' experience with SQL Server and Oracle databases as well as related technologies. His interests include database performance, HADR, and recently, Cloud.
Also, Kenneth teaches at Children's Church and writes fiction. You can connect with Kenneth via his blog or social network account.
Kenneth Igiri

Latest posts by Kenneth Igiri (see all)


Leave a Reply

Your email address will not be published. Required fields are marked *