Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
94 views

Comparing Null Values in SQL Server

This document discusses different ways to compare and evaluate fields with NULL values in SQL Server, including using the IS NULL clause to check if a column contains a NULL value, using the ISNULL function to default NULL values to a constant for joins, and using the COALESCE function to check multiple fields from left to right for a non-NULL value.

Uploaded by

nelrick1984
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Comparing Null Values in SQL Server

This document discusses different ways to compare and evaluate fields with NULL values in SQL Server, including using the IS NULL clause to check if a column contains a NULL value, using the ISNULL function to default NULL values to a constant for joins, and using the COALESCE function to check multiple fields from left to right for a non-NULL value.

Uploaded by

nelrick1984
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Nelrick Rodrigues

COMPARING NULL VALUES IN SQL SERVER


The examples below describe how to compare or evaluate fields having NULL values in SQL Server.
First, let us see an example what happens if NULL values are compared. For this, a simple CASE statement
is executed as shown below.

The Flag value in the above example is returned as a 0 which is incorrect, since if the value is NULL, a
value of 1 should be returned. This is a very simple test that proves that two NULL values cannot be
compared using the = operator.

Using the IS NULL clause:

The IS NULL clause is the simplest way to check if any column is having a NULL value. As seen above, the
Flag value of 1 is returned which is as expected.

Using the ISNULL function:

Nelrick Rodrigues
The ISNULL function is useful if you need to join between two columns that may contain NULL values. So,
even if you need the joins to happen between the NULL values for the two columns, you can default the
NULL value to a constant (0 in the above example) using the ISNULL function.

Using the COALESCE function:

The ISNULL function allows to specify only a single value that would be used in case the field contains a
NULL value. The COALESCE function, on the other hand allows you to specify multiple fields that will be
checked from left to right if they contain a NULL value. If the first field value is NULL, it will keep moving
to the next field on the right till it gets a field with a non-NULL value.

You might also like