Code has been added to clipboard!

Using the SQL ISNULL Function

Reading time 2 min
Published Aug 9, 2017
Updated Oct 10, 2019

SQL NULL Functions: Main Tips

  • The function ISNULL() is needed in order to define the way we use NULL.
  • There are other three functions which will produce a similar result ( COALESCE(), IFNULL(), NVL() ).

Syntax of SQL NULL

Below you can see the table "Products":

ID Name Supplier_ID Category_ID Unit Price Unit_In_Stock Unit_On_Order
1 Cucumber 3 3 5 crates 19 17 16
2 Tomato 3 3 6 crates 20 24 Null
3 Cheese 3 4 5 kg boxes 11 11 6
4 Milk 4 4 5 L bottles 22 56 12
5 Bread 4 4 36 boxes 26 26 1

Have in mind that UnitOnOrder has NULL values.

In the code example below we can see that the result will be NULL if the columns "Unit_On_Order" will contain NULL values:

Example
SELECT Name, Price * (Unit_In_Stock + Unit_On_Order)
FROM Products

The examples below show that if the column "UnitOnOrder" is a value NULL the enumeration will proceed as usual. The function ISNULL() brings back a zero when NULL is the value:

MS Access

Example
SELECT Name, Price * (Unit_In_Stock + IIF(ISNULL(Unit_On_Order), 0, Unit_On_Order))
FROM Products;

SQL Server

Example
SELECT Name, Price * (Unit_In_Stock + ISNULL(Unit_On_Order, 0))
FROM Products

Oracle

Here we start using other functions rather than ISNULL() because of the database-system compatibility. In oracle we use the function NVL():

Example
SELECT Name, Price * (Unit_In_Stock + NVL(Unit_On_Order, 0))
FROM Products

MySQL

Just like in oracle, MySQL requires a different function to work. There are two that work:

First, we have the function IFNULL():

Example
SELECT Name, Price * (Unit_In_Stock + IFNULL(Unit_On_Order, 0))
FROM Products

The second function is COALESCE():

Example
SELECT Name, Price * (Unit_In_Stock + COALESCE(Unit_On_Order, 0))
FROM Products
Learn SQL
Introduction
Syntax
Data Types
Server Data Types
Commands
Commands List
Wildcards
Constraints
Aggregate Functions
Date Functions
Date Format
Injection
SQL Server Hosting
Views
Auto-incrementation
SQL Operators
AS
AND & OR
IN
BETWEEN
WHERE
GROUP BY
HAVING
ORDER BY
LIKE
NOT
NOT EQUAL
UNION
NULL
NOT NULL
DEFAULT
UNIQUE
FOREIGN KEY
PRIMARY KEY
CHECK
Indexes
ALTER TABLE
CREATE DATABASE
CREATE TABLE
DELETE
DROP
INSERT INTO SELECT
INSERT INTO
SELECT
SELECT DISTINCT
SELECT INTO
SELECT TOP
UPDATE
FULL OUTER JOIN
INNER JOIN
JOIN
LEFT JOIN
RIGHT JOIN
AVG()
COUNT
FIRST
LAST
MAX
MIN()
SUM()
LEN
UCASE
MID
NOW
ROUND
FORMAT
LOWER
CONVERT
ISNULL