SQL ISNULL(), NVL(), IFNULL() and COALESCE() Functions

Look at the following "Products" table: P_Id ProductName UnitPrice UnitsInStock UnitsOnOrder ...

Look at the following "Products" table:

P_Id ProductName UnitPrice UnitsInStock UnitsOnOrder
1 Jarlsberg 10.45 16 15
2 Mascarpone 32.56 23
3 Gorgonzola 15.67 9 20

Suppose that the "UnitsOnOrder" column is optional, and may contain NULL values.
We have the following SELECT statement:

SELECT ProductName,UnitPrice*
(UnitsInStock+UnitsOnOrder)FROM Products

In the example above, if any of the "UnitsOnOrder" values are NULL, the result is NULL.
Microsoft's ISNULL() function is used to specify how we want to treat NULL values.
The NVL(), IFNULL(), and COALESCE() functions can also be used to achieve the same result.
In this case we want NULL values to be zero.
Below, if "UnitsOnOrder" is NULL it will not harm the calculation, because ISNULL() returns a zero if the value is NULL:

SQL Server / MS Access

SELECT ProductName,UnitPrice*(UnitsInStock+
ISNULL(UnitsOnOrder,0))FROM Products

Oracle

Oracle does not have an ISNULL() function. However, we can use the NVL() function to achieve the same result:

SELECT ProductName,UnitPrice*
(UnitsInStock+NVL(UnitsOnOrder,0))FROM Products

MySQL

MySQL does have an ISNULL() function. However, it works a little bit different from Microsoft's ISNULL() function.
In MySQL we can use the IFNULL() function, like this:

SELECT ProductName,UnitPrice*
(UnitsInStock+IFNULL(UnitsOnOrder,0))FROM Products

or we can use the COALESCE() function, like this:

SELECT ProductName,UnitPrice*
(UnitsInStock+COALESCE(UnitsOnOrder,0))FROM Products
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: SQL ISNULL(), NVL(), IFNULL() and COALESCE() Functions
SQL ISNULL(), NVL(), IFNULL() and COALESCE() Functions
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/sql-isnull-nvl-ifnull-and-coalesce.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/sql-isnull-nvl-ifnull-and-coalesce.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content