I know that multiple parameters can be passed to COALESCE
, but when you want to to
check just one expression to see if it doesn't exist, do you use a default or is it a better practice to use ISNULL
instead?
Is there any performance gain between the two?
In COALESCE you can have multiple expressions, where as in ISNULL you can check only one expression
In COALESCE one can use multiple expressions, It will return value which is not a null and occurs first... for example
And in ISNULL if expression null it will return second parameter provided, and of course you can check only for one expression...
So if want check multiple expression and select first not null among them, then use coalesce otherwise go for ISNULL
This problem reported on Microsoft Connect reveals some differences between
COALESCE
andISNULL
:This explanation gives clear about coalesce vs isnull
The COALESCE function in SQL returns the first non-NULL expression among its arguments. The syntax for COALESCE is as follows:
It is the same as the following CASE statement:
In SQL Server, the ISNULL( ) function is used to replace NULL value with another value.
Coalesce return first non null expression where as isnull() is used to replace null value with our desired value.
COALESCE is a part of ANSI standards and are available in almost all databases.
when deciding between ISNULL v COALESCE there parameters has to be taken care off:
Consider following sql statements
Results:
This happens because ISNULL takes the data type of the first argument, while COALESCE inspects all of the elements and chooses the best fit (in this case, VARCHAR(11))
For more detailed explanation on deciding between COALESCE vs ISNULL check this: https://www.mssqltips.com/sqlservertip/2689/deciding-between-coalesce-and-isnull-in-sql-server/
I think not, but COALESCE is in the SQL '92 standard and supported by more different databases. If you go for portability, don't use ISNULL.
The
NULL
andCOALESCE
are not always interchangeable. It deserves to know their differences in order to know when its better to use the one over the other:The table above is comparison between
ISNULL
andCOALESCE
fromExam Ref 70-761 Querying Data with Transact-SQL
book written by Itzik Ben-Gan.2
forISNULL
vs>2
when usingCOALESCE
ISNULL
is proprietary T-SQL feature andCOALESCE
is ISO/ANSI SQL standardThe data type of the result is important. After reading notes in the table above, check the following cases:
The
ISNULL
is getting the data type of the first argument as it is the notNULL
literal. It isVARCHAR(3)
and is a result, the second argument data is cut to match it. WithCOALESCE
the data type if highest precedence is used.The
ISNULL
is returning the data type of first argument, while inCOALESCE
we are getting error, as theINT
has highest precedence and the conversion of the first argument value toINT
fails.The nullability of the result can be important, too. For, example:
Let's check the
Nullable
property of each column:Using
COALESCE
we have aNOT NULL
property of column set toYes
, only when all of the inputs are non null-able.According to the SQL standard, the
COALESCE
expression is translated to: