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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 COALESCE vs case vs if vs is null

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2007-01-24 : 08:17:56
ryan writes "Just wondering what will execute quicker in ms-sql



COALESCE
case
if
is null

Something like the following:

WHERE

o.blnDeleted = 0
and o2l.strOfficeName=COALESCE(@officename,o2l.strOfficeName)"

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-01-24 : 08:19:53
isnull is fastest.
coalescse acctually translates into case's



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-01-24 : 08:20:19
From the example you gave, IF is the most appropriate performance wise and will generate much better plan than any other option.

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

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-01-24 : 08:22:41
he gave an example? where?



Go with the flow & have fun! Else fight the flow
blog thingie: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-01-24 : 18:14:09
IF @OfficeName IS NULL
select ...
ELSE
select ... where o2l.strOfficeName = o2l.strOfficeName


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2007-01-24 : 22:47:13
Depends what you are doing. You can only ever test performance of things that are not the same against a specific case. See which one is fastest by trying each one and measuring it. If there was a hard & fast answer then one would assume that either the optimiser would simply use it all the time or the language designers would remove the options completely, no?
Go to Top of Page
   

- Advertisement -