What is the difference between COALESCE, NULLIF, IFNULL and ISNULL?
What is the difference between COALESCE, NULLIF, IFNULL and ISNULL?
All of them handle NULL, but they differ in portability and behaviour.
| Function | Meaning | Where |
|---|---|---|
COALESCE(a, b, c, ...) |
first non-NULL argument |
SQL standard, all major engines |
NULLIF(a, b) |
NULL if a = b, otherwise a |
standard |
IFNULL(a, b) |
a if not NULL, else b |
MySQL, MariaDB, SQLite |
ISNULL(a, b) |
same, two arguments | SQL Server |
NVL(a, b) / NVL2 |
same, two arguments | Oracle |
SELECT COALESCE(nickname, first_name, 'Guest') FROM users; -- first available name
SELECT total / NULLIF(quantity, 0) FROM line_items; -- avoids division by zero (gives NULL)
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.