Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

coalesce and nullif Expand / Collapse
Author
Message
Posted Tuesday, June 22, 2010 9:19 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, January 30, 2012 5:12 AM
Points: 40, Visits: 143
Comments posted to this topic are about the item coalesce and nullif
Post #941465
Posted Tuesday, June 22, 2010 9:39 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, November 24, 2014 8:33 AM
Points: 1,676, Visits: 1,760
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
Post #941470
Posted Wednesday, June 23, 2010 12:59 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, January 3, 2014 12:02 AM
Points: 356, Visits: 71
Thanks for explain precedence logic. Now I understand it perfectly
Post #941534
Posted Wednesday, June 23, 2010 1:10 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:54 PM
Points: 6,130, Visits: 8,394
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
Post #941539
Posted Wednesday, June 23, 2010 1:15 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Friday, May 9, 2014 12:47 AM
Points: 3,448, Visits: 4,407
Nice question.

OFF
I would like thanks my coorker...

What does the word 'coorker' mean? Is it a typo and needs to be 'coworker'?
Post #941540
Posted Wednesday, June 23, 2010 1:46 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, November 5, 2013 5:11 AM
Points: 1,229, 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...

Post #941568
Posted Wednesday, June 23, 2010 5:30 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 6:41 AM
Points: 906, Visits: 892
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!

-----
a haiku...

NULL is not zero
NULL is not an empty string
NULL is the unknown
Post #941657
Posted Wednesday, June 23, 2010 6:30 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, January 30, 2012 5:12 AM
Points: 40, 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
Post #941701
Posted Wednesday, June 23, 2010 7:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:24 PM
Points: 5,370, Visits: 9,009
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
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
Post #941729
Posted Wednesday, June 23, 2010 8:24 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, September 14, 2010 8:24 AM
Points: 69, Visits: 33
null date can be cast as null string, but not the other way, interesting.
Post #941799
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse