Summary: in this tutorial, you will learn how to use the SQL NULLIF function to compare two values and return NULL if they are equal.
Introduction to the SQL NULLIF function #
The NULLIF function compares two values and returns NULL if they are equal.
Here’s the syntax of the NULLIF function:
NULLIF(value1,value2);
The NULLIF function returns NULL if value1 equals value2. It returns value1 if the values are not equal.
The NULLIF function is equivalent to the following searched CASE expression:
CASE
WHEN value1 = value1 THEN NULL
ELSE value1
END
Basic SQL NULLIF function examples #
The following example uses the NULLIF function with two arguments are 100:
SELECT
NULLIF(100, 100) result;
Output:
result
--------
NULL
The following example returns the first argument which is 100 because 100 is not equal to 200:
SELECT
NULLIF(100, 200) result;
result
--------
100
The following statement returns the first argument which is 100 because 100 is not equal to NULL.
SELECT
NULLIF(100, NULL) result;
Output:
result
--------
100
The following statement returns the first argument which is NULL because NULL is not equal to 100:
SELECT
NULLIF(NULL, 100) result;
Output:
result
--------
NULL
The following statement returns the NULL because the string SQL is equal to the string SQL:
SELECT
NULLIF('SQL', 'SQL') result;
Output:
result
--------
NULL
Using NULLIF function with table data #
First, create a new table calledarticles to store articles:
CREATE TABLE articles (
article_id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
excerpt VARCHAR(255),
body TEXT
);
Second, insert some rows into the articles table:
INSERT INTO
articles (article_id, title, excerpt, body)
VALUES
(
1,
'SQL NULLIF function',
'',
'This tutorial shows you how to use the SQL NULLIF function'
),
(
2,
'SQL tutorial',
'Learn how to use SQL at sqltutorial.org',
'You will learn SQL with practical examples'
),
(
3,
'SQL query',
NULL,
'You will learn how to use SELECT statement to query data from tables'
);
Third, retrieve article_id, title and excerpt from the articles table:
SELECT
article_id,
title,
excerpt
FROM
articles;
Output:
article_id | title | excerpt
------------+---------------------+-----------------------------------------
1 | SQL NULLIF function |
2 | SQL tutorial | Learn how to use SQL at sqltutorial.org
3 | SQL query | NULL
If the excerpt is not available, we can use the first 50 characters of the body instead.
To do that, we use the COALESCE function to return excerpt column if it is not NULL, otherwise, return the first 50 characters of the body.
SELECT
article_id,
title,
COALESCE(excerpt, LEFT(body, 50)) AS summary
FROM
articles;
Output:
article_id | title | summary
------------+---------------------+----------------------------------------------------
1 | SQL NULLIF function |
2 | SQL tutorial | Learn how to use SQL at sqltutorial.org
3 | SQL query | You will learn how to use SELECT statement to quer
However, The summary column is empty for the article id 1. To solve this problem, as you may guess, we use the NULLIF function.
SELECT
article_id,
title,
COALESCE(NULLIF(excerpt,''), LEFT(body, 50)) AS summary
FROM
articles;
Output:
article_id | title | summary
------------+---------------------+----------------------------------------------------
1 | SQL NULLIF function | This tutorial shows you how to use the SQL NULLIF
2 | SQL tutorial | Learn how to use SQL at sqltutorial.org
3 | SQL query | You will learn how to use SELECT statement to quer
If the excerpt is empty, the NULLIF function returns NULL, otherwise it returns the excerpt. Then the rest is handled by the COALESCE function.
Summary #
- Use the
NULLIFfunction to compare two values and returnsNULLif they are equal.
Quiz #
SQL NULLIF Function
Databases #
- PostgreSQL NULLIF Function
- MySQL NULLIF Function
- SQLite NULLIF Function
- Oracle NULLIF Function
- SQL Server NULLIF Function
Thank you for your feedback!