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
 coalesce and nullif

Author  Topic 

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-02-09 : 03:12:31
hello,

from one of my solution, i havent had time untill now to ask detail what is this coalesce and nullif? Is nullif just like the isnull function? while coalesce is to replace null? can someone explain base on this eg??

d.LocID >= coalesce(nullif(@LocFrom, ''), d.LocID) and
d.LocID <= coalesce(nullif(@LocTo, ''), d.LocID) and

~~~Focus on problem, not solution~~~

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-02-09 : 03:14:36
From Books online:

quote:
NULLIF
Returns a null value if the two specified expressions are equivalent.



quote:
COALESCE
Returns the first nonnull expression among its arguments.


So from your example:

NULLIF(@LocFrom, '') - returns NULL if @LocFrom = ''
Coalesce(NULLIF(@LocFrom, ''), d.LocID) - will return @LocFrom if it is not '', else it will return d.LocID in effect satisfying the WHERE clause always.

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-02-09 : 03:26:13
ooo thanks.. the nullif is clear, the coalesce is still quite blur... when do u use coalesce? so if @LocFrom='' and @LocTo='' then is actually

d.LocID >= coalesce(null, d.LocID) and
d.LocID <= coalesce(null, d.LocID) and

this where clause find non null of column d.locid?? all excluding null isit??? but how about this one??

COALESCE(UDF10, '%') LIKE '%'+@UDF10+'%'


~~~Focus on problem, not solution~~~
Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-02-09 : 03:34:00
coalesce accept 2 args(1st,2nd) what is 1st type of data and 2nd type of data? how do we manipulate it... sorry if this quite silly Q :P

~~~Focus on problem, not solution~~~
Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-02-09 : 03:39:10
what it return if both args is NULL? thanks

~~~Focus on problem, not solution~~~
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-09 : 03:42:25
run this and see the result.

declare @arg1 int,
@arg2 int,
@arg3 int

select @arg1 = NULL, @arg2 = NULL, @arg3 = NULL
select arg1 = @arg1, arg2 = @arg2, arg3 = @arg3, [coalesce(@arg1, @arg2, @arg3)] = coalesce(@arg1, @arg2, @arg3)

select @arg1 = NULL, @arg2 = 2, @arg3 = 3
select arg1 = @arg1, arg2 = @arg2, arg3 = @arg3, [coalesce(@arg1, @arg2, @arg3)] = coalesce(@arg1, @arg2, @arg3)

select @arg1 = 1, @arg2 = 2, @arg3 = 3
select arg1 = @arg1, arg2 = @arg2, arg3 = @arg3, [coalesce(@arg1, @arg2, @arg3)] = coalesce(@arg1, @arg2, @arg3)



KH

Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-02-09 : 03:49:55
wow khtan!!
that really helps :D ... no more blurs

~~~Focus on problem, not solution~~~
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-09 : 03:52:30
just a note. If your front end application is able to pass in NULL instead of '' when the user did not select anything, wouldn't it make thing slightly easier ? Then you do not have to use the nullif() to check.


KH

Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-02-09 : 20:03:49
yup.. havent think of that :P, but i already practic it in my dates param only coz it cannot compare date as varchar

~~~Focus on problem, not solution~~~
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-02-10 : 08:40:12
COALESCE is Ansi standard.

ISNULL is not.

COALESCE can take multiple parameters (it returns the first one which is NOT null, it returns null if ALL the parameters are null)

ISNULL can only take two parameters.

For my this means that I always use COALESCE - plus "IsNull" as a function name suggests to me something that will test if an expression is NULL and return True or False.

NullIf() is something else as Khtan has explained.

Kristen
Go to Top of Page
   

- Advertisement -