SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


coalesce and nullif


coalesce and nullif

Author
Message
LP-181697
LP-181697
SSC-Enthusiastic
SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)

Group: General Forum Members
Points: 132 Visits: 143
Comments posted to this topic are about the item coalesce and nullif
Oleg Netchaev
Oleg Netchaev
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1795 Visits: 1814
This is a very good question, thank you. Presence of nullif does not change anything though, so it is difficult to figure out why it is there. What I mean is that if the question is reduced to declaring and setting @c and @d only and the statements in question are then changed to

select coalesce(@c, @d);

select coalesce(@d, @c);



the result will still be the same, the first statement will fail and the second one will run just fine. I guess that this is because of the major difference between the behavior of coalesce with 2 parameters and isnull function which I believe would execute both statements just fine: coalesce always returns the expression of the highest precedence data type regardless of the parameters' order while isnull returns the data type of the first parameter. In other words, coalesce tries to cast first occurrence of the not null as datetime (datetime is of higher precedence). This is why it fails in the first statement, the first not null is @c which is equal to 'test', it tries to cast 'test' as datetime and fails. The second statement has the first not null is @d which is a datetime already, so the second statement executes without error.

Once again, thank you for an excellent question, I really enjoyed it.

Oleg
hemang.patel
hemang.patel
Old Hand
Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)Old Hand (356 reputation)

Group: General Forum Members
Points: 356 Visits: 71
Thanks for explain precedence logic. Now I understand it perfectly
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11180 Visits: 12004
Thanks, Oleg, for the explanation of the difference between COALESCE and ISNULL. I was about to add it myself, since it is lacking in the question, but you beat me to it.

And I agree with you that the NULLIF serves no apparent function here. Other than to obfuscate. (I knew that both COALESCE functions would return datetime; I then pondered for an extra minute to make really sure that the NULLIF does not affect the result at all; then gave the wrong answer because my not-yet awake brain thought both expression would try to convert 'Test' to the target data type).

And for those who want to do some further reading:
* Data type precedence: http://msdn.microsoft.com/en-us/library/ms190309.aspx
* NULLIF: http://msdn.microsoft.com/en-us/library/ms177562.aspx
* COALESCE: http://msdn.microsoft.com/en-us/library/ms190349.aspx
* ISNULL (similar to, yet also different from COALESCE): http://msdn.microsoft.com/en-us/library/ms184325.aspx


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
vk-kirov
vk-kirov
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3732 Visits: 4408
Nice question.

OFF
I would like thanks my coorker...

What does the word 'coorker' mean? Is it a typo and needs to be 'coworker'?
Arsi Salo
Arsi Salo
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1233 Visits: 193
Nice QotD. It would have been more educational with third section
select @c = 'test', @c1 = null, @d = '01/01/10', @d1 = '01/01/10'

,
which brings nullif into fray...
ronmoses
ronmoses
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1249 Visits: 996
This is a great question, and I'm proud that I managed to work out the correct answer once I realized the first statement was going to try and cast 'test' as a datetime. And then I clicked the wrong option and lost the points. Yay for me! :-D

-----
a haiku...

NULL is not zero
NULL is not an empty string
NULL is the unknown

LP-181697
LP-181697
SSC-Enthusiastic
SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)SSC-Enthusiastic (132 reputation)

Group: General Forum Members
Points: 132 Visits: 143
vk-kirov (6/23/2010)
Nice question.

OFF
I would like thanks my coorker...

What does the word 'coorker' mean? Is it a typo and needs to be 'coworker'?


It means coworker. It was typo
WayneS
WayneS
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10066 Visits: 10574
Oleg, thanks for the explanation. Hugo, thanks for the links, especially for the data type precedence. Between the two of you'll, this is the explanation that the question should have had.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

forjonathanwilson
forjonathanwilson
SSC Journeyman
SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)SSC Journeyman (75 reputation)

Group: General Forum Members
Points: 75 Visits: 33
null date can be cast as null string, but not the other way, interesting.Hehe
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search