'What is the difference between IFNULL and COALESCE in MySQL?
SELECT IFNULL(NULL, 'Replaces the NULL')
--> Replaces the NULL
SELECT COALESCE(NULL, NULL, 'Replaces the NULL')
--> Replaces the NULL
In both clauses the main difference is argument passing. For IFNULL it's two parameters and for COALESCE it's multiple parameters. So except that, do we have any other difference between these two?
And how it differs in MS SQL?
Solution 1:[1]
Pros of COALESCE
COALESCEis SQL-standard function.While
IFNULLis MySQL-specific and its equivalent in MSSQL (ISNULL) is MSSQL-specific.COALESCEcan work with two or more arguments (in fact, it can work with a single argument, but is pretty useless in this case:COALESCE(a)?a).While MySQL's
IFNULLand MSSQL'sISNULLare limited versions ofCOALESCEthat can work with two arguments only.
Cons of COALESCE
Per Transact SQL documentation,
COALESCEis just a syntax sugar forCASEand can evaluate its arguments more that once. In more detail:COALESCE(a1, a2, …, aN)?CASE WHEN (a1 IS NOT NULL) THEN a1 WHEN (a2 IS NOT NULL) THEN a2 ELSE aN END. This greatly reduces the usefulness ofCOALESCEin MSSQL.On the other hand,
ISNULLin MSSQL is a normal function and never evaluates its arguments more than once.COALESCEin MySQL and PostgreSQL neither evaluates its arguments more than once.At this point of time, I don't know how exactly SQL-standards define
COALESCE.As we see from previous point, actual implementations in RDBMS vary: some (e.g. MSSQL) make
COALESCEto evaluate its arguments more than once, some (e.g. MySQL, PostgreSQL) — don't.c-treeACE, which claims it's
COALESCEimplementation is SQL-92 compatible, says: "This function is not allowed in a GROUP BY clause. Arguments to this function cannot be query expressions." I don't know whether these restrictions are really within SQL-standard; most actual implementations ofCOALESCE(e.g. MySQL, PostgreSQL) don't have such restrictions.IFNULL/ISNULL, as normal functions, don't have such restrictions either.
Resume
Unless you face specific restrictions of COALESCE in specific RDBMS, I'd recommend to always use COALESCE as more standard and more generic.
The exceptions are:
- Long-calculated expressions or expressions with side effects in MSSQL (as, per documentation,
COALESCE(expr1, …)may evaluateexpr1twice). - Usage within
GROUP BYor with query expressions in c-treeACE. - Etc.
Solution 2:[2]
Differences in SQL-Server:
There is no
IFNULL()function but a similarISNULL()ISNULLtakes only 2 parameters whereas COALESCE takes variable number of parametersCOALESCEis based on the ANSI SQL standard whereasISNULLis a proprietary TSQL functionValidations for
ISNULLandCOALESCEis also different. For example,NULLvalue forISNULLis converted to int, whereas forCOAELSCEyou have to provide a type. Ex:ISNULL(NULL,NULL): is int.COALESCE(NULL,NULL): will throw an error.COALESCE(CAST(NULL as int),NULL): is valid and returns int.
Data type determination of the resulting expression –
ISNULLuses the first parameter type,COALESCEfollows theCASEexpression rules and returns type of value with highest precedence.
Solution 3:[3]
ifnull can only replace a null value of the first parameter. Whereas coalesce can replace any value with another value. With coalesce in standard SQL you can have many parameters transforming many values.
EDIT the example according to comments below.
Example: coalesce(null, null, null, 'b*', null, 'null*')
returns 'b*' and it is not possible to do with ifnull.
Solution 4:[4]
This db2 SQL will not work with COALESE, I will not see any rows retrieved. Since I used IFNULL it is working as expected
select a.mbitno ,a.mbstqt,ifnull(b.apr,0)
from
(
select mmstcd,mbstat,mbfaci,mbwhlo,mbitno,mbstqt,MBALQT from libl.mitbal inner join libl.mitmas on
mmcono=mbcono and mmitno=mbitno
where mbcono=200 and mbstat in ('20','50') and mmstcd>0
)
as a left join
(
select mlfaci,mlwhlo,mlitno,mlstas,sum(mlstqt) as APR from libl.mitloc where mlcono=200 and mlstas='2'
group by mlfaci,mlwhlo,mlitno,mlstas
)
b on b.mlfaci=a.mbfaci and b.mlwhlo=a.mbwhlo and b.mlitno=a.mbitno
where a.mbitno in 'GWF0240XPEC' and a.mbstqt>0 and a.mbstqt<>ifnull(b.apr,0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cmaher |
| Solution 2 | ypercubeᵀᴹ |
| Solution 3 | |
| Solution 4 | ankit suthar |
