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

Source: Internet
Author: User

SQL isnull (), NVL (), IFNULL () and COALESCE () functions are commonly used in SQL statements. Let's take a look at the example tutorial.

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


Assume that the "UnitsOnOrder" column is optional and may contain null values.

We have the following SELECT statements:
SELECT ProductName, UnitPrice * (UnitsInStock + UnitsOnOrder)
FROM Products
In the preceding example, if any "UnitsOnOrder" value is null, the result is invalid. Microsoft's ISNULL () function is used to specify how to handle null values. The NVL (), IFNULL () and Union () functions can also be used to achieve the same result. In this case, the null value is zero. Below, if "UnitsOnOrder" is NULL, it will not damage the calculation because ISNULL () returns a zero value if it is NULL: SQL Server/MS AccessSELECT ProductName, unitPrice * (UnitsInStock + ISNULL (UnitsOnOrder, 0 ))
FROM Products
 
Oracle
Oracle does not have the ISNULL () function. However, we can use the NVL () function to implement the same results SELECT ProductName, UnitPrice * (UnitsInStock + NVL (UnitsOnOrder, 0 ))
FROM Products
 
Mysql
 
MySQL does have an ISNULL () function. However, its works are a little different. Microsoft's ISNULL () function. In MySQL, we can use the IFNULL () function, just like this: SELECT ProductName, UnitPrice * (UnitsInStock + IFNULL (UnitsOnOrder, 0 ))
FROM Products

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.