SQL IFNULL(), ISNULL(), COALESCE(), And NVL() Functions
Có thể bạn quan tâm
SQL IFNULL(), ISNULL(), COALESCE(), and NVL() Functions
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.
Look at 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 will be NULL.
Solutions
MySQL
The MySQL IFNULL() function lets you return an alternative value if an expression is NULL:
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;SQL Server
The SQL Server ISNULL() function lets you return an alternative value when an expression is NULL:
SELECT ProductName, UnitPrice * (UnitsInStock + ISNULL(UnitsOnOrder, 0)) FROM Products;or we can use the COALESCE() function, like this:
SELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0)) FROM Products;MS Access
The MS Access IsNull() function returns TRUE (-1) if the expression is a null value, otherwise FALSE (0):
SELECT ProductName, UnitPrice * (UnitsInStock + IIF(IsNull(UnitsOnOrder), 0, UnitsOnOrder)) FROM Products;Oracle
The Oracle NVL() function achieves the same result:
SELECT ProductName, UnitPrice * (UnitsInStock + NVL(UnitsOnOrder, 0)) FROM Products;or we can use the COALESCE() function, like this:
SELECT ProductName, UnitPrice * (UnitsInStock + COALESCE(UnitsOnOrder, 0)) FROM Products; ❮ Previous Next ❯ ★ +1 Sign in to track progressTừ khóa » Câu Lệnh Nvl Trong Sql
-
Hai Ví Dụ Thực Tế Về Chức Năng NVL Của Oracle
-
Các Hàm SQL ISNULL (), NVL (), IFNULL () Và COALESCE ()
-
Hàm NVL – Oracle/PLSQL | Học Toàn Tập
-
[Tự Học SQL] Tìm Hiểu Về Giá Trị NULL Và Hàm IFNULL(), ISNULL ...
-
SQL NULL Functions - TRẦN VĂN BÌNH MASTER
-
Chức Năng Oracle - Go Coding
-
Câu Lệnh COALESCE Trong SQL Server
-
Sql — Cột Tổng Với Giá Trị Null Trong Orory - Wake-up
-
NVL
-
Oracle: Điều Kiện Về Biến Trong Mệnh đề Where
-
Phân Tích Các Hàm Cơ Bản Trong Oracle - TaiLieu.VN
-
<4 - MofunIT