Wednesday 9 March 2016

ISNULL VS COALESCE ?



ISNULL function is a Transact-SQL function while COALESCE function in an ANSI SQL standard.
ISNULL function contains only two parameters while COALESCE function contains multiple parameters.

Example:- 
Select ISNULL(null,'Hello')
Select coalesce(null,null,'hello')

ISNULL function looks at first value and second parameter value is automatically limit to the length of first parameter but COALESCE do not have this restriction.


Example:- 

Declare @test varchar(4)
Select ISNULL(@test,'abcde') – give abcd
Select coalesce(@test,'abcde')—give  abcde

ISNULL function contains different type of data types while COALESCE function parameter should have same datatype.


Example:- 

Declare @a varchar(5)='abcd'
Declare @b int=5
Select ISNULL(@a,@b)--- compiled
Select coalesce(@a,@b) --- give error

No comments:

Post a Comment

Note: only a member of this blog may post a comment.