bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL NULL Functions

SQL COALESCE(), IFNULL(), ISNULL(), and NVL() Functions Operations involving NULL values can sometimes lead to unexpected results.

Formula

SQL has some built - in functions to handle NULL values, and the most common functions are:

Coalesce()

  • The preferred standard. (Works in MySQL, SQL Server and Oracle)

Ifnull()

  • (MySQL)

Isnull()

  • (SQL Server)

Nvl()

  • (Oracle)

IsNull()

  • (MS Access)

Note:

A Null

value represents an unknown or missing data in a database field. It is not a value itself, but a placeholder to indicate the absence of data.

Demo Database

Assume we have the following "Products" table:

PId

ProductName

Price

InStock

InOrder

Jarlsberg

10.45 16 15

Mascarpone

32.56 23 null

Gorgonzola

15.67

20 The "InOrder" column is optional, and may contain NULL values. Now look at the following SQL statement:

SELECT ProductName, Price * (InStock + InOrder)
FROM Products;

Note

In the SQL above, if any of the "InOrder" values are NULL, the result will be NULL!

The COALESCE() Function

The COALESCE() function is the preferred standard for handling potential NULL values.

Formula

The COALESCE() function returns the first non - NULL value in a list of values.

The COALESCE() function works in

MySQL, SQL Server, and Oracle (not in MS Access).

Syntax

Coalesce(

val1, val2,...., val_n ) Here we use the COALESCE() function to replace NULL values with 0:

SELECT ProductName, Price * (InStock + COALESCE(InOrder, 0))
FROM Products;
The IFNULL() Function (MySQL)

The MySQL

IFNULL() function replaces NULL with a specified value.

Syntax

Ifnull(

expr, alt ) Here we replace NULL values with 0:

SELECT ProductName, Price * (InStock + IFNULL(InOrder, 0))
FROM Products;
The ISNULL() Function (SQL Server)

Previous

SQL CASE Expression

Next

SQL Stored Procedures