|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, January 30, 2012 5:12 AM
Points: 40,
Visits: 143
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 10:53 AM
Points: 1,662,
Visits: 1,709
|
|
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
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Tuesday, November 20, 2012 1:42 AM
Points: 236,
Visits: 41
|
|
| Thanks for explain precedence logic. Now I understand it perfectly
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:03 PM
Points: 5,244,
Visits: 7,061
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 6:30 AM
Points: 3,192,
Visits: 4,151
|
|
Nice question.
OFF
I would like thanks my coorker... What does the word 'coorker' mean? Is it a typo and needs to be 'coworker'?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, March 21, 2013 7:13 AM
Points: 1,228,
Visits: 190
|
|
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...
|
|
|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Today @ 10:36 AM
Points: 651,
Visits: 688
|
|
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
|
|
|
|
|
SSC 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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 9:12 AM
Points: 6,370,
Visits: 8,235
|
|
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
|
|
|
|
|
Valued 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.
|
|
|
|