SQL Server NULL

Learning Objective

The objective of this SQL Server tutorial is to teach you how to evaluate a value and determine whether it is NULL.

What is NULL in SQL Server?

The NULL value in SQL represents the absence of value. It is different from zero (0) or empty space (‘’). It simply means that there is no value for a field or column in a record. Since a NULL value signifies nothing it cannot be evaluated using the normal logical operators (i.e. =, !=, >, <). This is because NULL does not equal anything nor is it greater or less than something. Hence when evaluated using any of the logical operators it evaluates to UNKNOWN.

To address this issue SQL has the IS NULL operator which evaluates a value to determine whether it is NULL or not. If it is then the IS NULL expression returns TRUE otherwise FALSE. There is also an IS NOT NULL operator which performs just the opposite and returns TRUE when a value is not NULL and returns FALSE when it is.

Operation

The IS NULL operator is used in a logical expression with the WHERE clause when we need to evaluate and determine whether a column value is NULL or not. The converse logic applies for the IS NOT NULL operator. It can be used with SELECT, UPDATE and DELETE.

Syntax

The basic syntax of IS NULL (and IS NOT NULL) is given below.

WHERE expression IS NULL;
WHERE expression IS NOT NULL;

In this syntax,

  • expression – something that returns a unique value (can be a literal value itself or a column, parameter, variable, subquery, mathematical operation, function etc.).
  • IS NULL – operator used to check and determine whether a value is NULL.
  • IS NOT NULL– operator used to check and determine that a value is not NULL (i.e. a valid value).

SQL Server IS NULL Examples

To understand its practical usage let us consider the following sample customers table which contains customer information. It consists of a number of columns or fields some of which are optional and contains NULL values for some records.

idnamesexcityphone
1Stephen Fleming MPhiladelphia16107575525
5John WellsMNew YorkNULL
7Alice RossiFNULLNULL
8Jack CrowMLos Angeles16616165325
9Laura SmithFNULL16633775159
10Vicky WilliamsFNULLNULL
11David MathewsMWashington16277212992
12Richard AndersonMWashington16277212992
13Jason KinghtMNULLNULL
14Patricia ChristFPhiladelphiaNULL
Table Name:  customers

1)  SQL Server NULL– IS NULL example

This is where the IS NULL operator comes into play. Using the same we can determine those records where the customer city is not available using the following query.

SELECT *
FROM customers
WHERE city IS NULL;

 It will generate the following output where the value for the column city is NULL (or not known).

idnamesexcityphone
7Alice RossiFNULLNULL
9Laura SmithFNULL16633775159
10Vicky WilliamsFNULLNULL
13Jason KinghtMNULLNULL

We can also check for both city and phone at the same (i.e. check records with NULL values for all the optional columns in a table) with the following query combining multiple IS NULL expressions.

SELECT *
FROM customers
WHERE city IS NULL
AND phone IS NULL;

It will generate the following output where the value for both columns is NULL.

idnamesexcityphone
7Alice RossiFNULLNULL
10Vicky WilliamsFNULLNULL
13Jason KinghtMNULLNULL

2)  SQL Server NULL– IS NOT NULL example

Now let us see an example using the IS NOT NULL operator to fetch those values from the table where the phone numbers are NOT NULL (i.e. where the customer phone numbers are available). The below query does the same.

SELECT *
FROM customers
WHERE city IS NOT NULL;
idnamesexcityphone
1Stephen Fleming MPhiladelphia16107575525
8Jack CrowMLos Angeles16616165325
9Laura SmithFNULL16633775159
11David MathewsMWashington16277212992

Advertisement