bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
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.

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:

PIdProductNamePriceInStockInOrder
1Jarlsberg10.451615
2Mascarpone32.5623null
3Gorgonzola15.67920

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.

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)

The SQL Server ISNULL() function replaces NULL with a specified value.

Syntax

ISNULL(
expr
,
alt
)

Here we replace NULL values with 0:

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

The NVL() Function (Oracle)

The Oracle NVL() function replaces NULL with a specified value.

Syntax

NVL(
expr
,
alt
)

Here we replace NULL values with 0:

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

The IsNull() Function (MS Access)

The MS Access IsNull() function returns TRUE if the expression is NULL, otherwise FALSE.

Syntax

IsNull(
expr
)

The MS Access IIf() function returns one of two parts, depending on the evaluation of the expression.

Syntax

IIf(
expr, truepart, falsepart
)
  • expr - Required. The expression to evaluate
  • truepart - Value to return if expr is True
  • falsepart - Value to return if expr is False

Here we replace NULL values with 0:

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

Previous

SQL CASE Expression

Next

SQL Stored Procedures

This chapter

Overview
46

Lessons

115m

Read time

1. SQL Tutorial2. Introduction to SQL3. SQL Syntax4. SQL SELECT Statement5. SQL SELECT DISTINCT Statement6. SQL WHERE Clause7. SQL ORDER BY Keyword8. SQL AND Operator9. SQL OR Operator10. SQL NOT Operator11. SQL INSERT INTO Statement12. SQL NULL Values13. SQL UPDATE Statement14. SQL DELETE Statement15. SQL SELECT TOP, LIMIT and FETCH FIRST16. SQL Aggregate Functions17. SQL MIN() F unction18. SQL MAX() Function19. SQL COUNT() Function20. SQL SUM() Function21. SQL AVG() Function22. SQL LIKE Operator23. SQL Wildcards24. SQL IN Operator25. SQL BETWEEN Operator26. SQL Aliases27. SQL Joins28. SQL INNER JOIN29. SQL LEFT JOIN30. SQL RIGHT JOIN31. SQL FULL JOIN32. SQL Self Join33. SQL UNION Operator34. SQL UNION ALL Operator35. SQL GROUP BY Statement36. SQL HAVING Clause37. SQL EXISTS Operator38. SQL ANY Operator39. SQL ALL Operator40. SQL SELECT INTO Statement41. SQL INSERT INTO SELECT Statement42. SQL CASE Expression43. SQL NULL Functions44. SQL Stored Procedures45. SQL Comments46. SQL Operators

On this page

SQL COALESCE(), IFNULL(), ISNULL(), and NVL() FunctionsDemo DatabaseThe COALESCE() FunctionThe IFNULL() Function (MySQL)The ISNULL() Function (SQL Server)The NVL() Function (Oracle)The IsNull() Function (MS Access)