Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 What Is th difference between Is Null & COALESCE

Author  Topic 

arorarahul.0688
Posting Yak Master

125 Posts

Posted - 2008-02-04 : 09:08:30
WHATS the basic diff. between Is Null and Coalesce can someone help me regarding this

Rahul Arora
07 Batch
NCCE Israna,


######################
IMPOSSIBLE = I+M+POSSIBLE

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-02-04 : 09:12:54
Keyword "IS NULL" is a check to see if value IS NULL.
Function "ISNULL()" returns second parameter is first parameter IS NULL.
Function "COALESCE()" returns first parameter that NOT IS NULL.

Read Books Online for more in-depth explanation.




E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-04 : 09:26:11
Adding to above:-

COALESCE works the same way as CASE WHEN. As such it requires all its expressions to be of same datatype. execute this and see:-
Print COALESCE('An String','12')
and also

Print COALESCE('An String',12)

Whereas you can use ISNULL to work like this:-

Print ISNULL('An String',12)

But problem with ISNULL is the return datatype will same as type of first expression. See this:-

DECLARE @Str varchar(5)
Print ISNULL(@Str,'cdwfergthtyjukuikil')
this will return only first 5 characters as @str is of type varchar(5).

This is clearly suggested in BOL as:-

The value of check_expression is returned if it is not NULL; otherwise, replacement_value is returned after it is implicitly converted to the type of check_expression, if the types are different.
and
see the definition of COALESCE which says:-

If all arguments are NULL, COALESCE returns NULL.

COALESCE(expression1,...n) is equivalent to this CASE function:

CASE
WHEN (expression1 IS NOT NULL) THEN expression1
...
WHEN (expressionN IS NOT NULL) THEN expressionN
ELSE NULL
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-28 : 07:39:40
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/04/isnull-or-coalesce.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -