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 Wednesday, June 23, 2010 8:32 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:07 PM
Points: 6,002, Visits: 8,266
forjonathanwilson (6/23/2010)
null date can be cast as null string, but not the other way, interesting.

This is not correct. A null string can be cast as date (result will be NULL). The problem starts when you try to case a non-null string as date - in that case, the string must evaluate to a valid date. And obviously, the string 'Test' does not qualify there.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #941807
Posted Wednesday, June 23, 2010 9:12 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
Hugo Kornelis (6/23/2010)
forjonathanwilson (6/23/2010)
null date can be cast as null string, but not the other way, interesting.

This is not correct. A null string can be cast as date (result will be NULL). The problem starts when you try to case a non-null string as date - in that case, the string must evaluate to a valid date. And obviously, the string 'Test' does not qualify there.


thanks for clarifying that for me. I also checked this myself after I made my irroneous assumption:

declare @c varchar(10), @c1 varchar(10),
@d datetime, @d1 datetime
select @c = 'test', @c1 = null, @d = '1/1/10', @d1 = null

select coalesce(nullif(@c,@c),nullif(@d,@d))

select coalesce(nullif(@d,@d),nullif(@c,@c))

where the results are compared null datetime to null string by coalesce, which completes just fine.



Post #941854
Posted Wednesday, June 23, 2010 11:10 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 5, 2014 2:00 PM
Points: 2,163, Visits: 2,191
Interesting question, thanks.

Thanks Oleg for the explanation of why it happens, I think most people don't already know that, so it is very helpful.
Post #941951
Posted Thursday, June 24, 2010 2:39 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, September 12, 2014 5:21 PM
Points: 2,262, Visits: 5,406
Oleg and Hugo , thanks for the nice explanations and links.. nice question depicting the precedence of data-types... niceee.
Post #942298
Posted Thursday, June 24, 2010 7:18 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, September 8, 2014 7:06 PM
Points: 528, Visits: 1,260
thanks for the good QOTD and explanation behind it.
Post #942781
Posted Monday, June 28, 2010 12:35 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 7:35 AM
Points: 2,819, Visits: 2,563
Good question, thanks. The answer logic seems backwards to me. I think I am going to have to do a little more reading.
Post #944161
Posted Wednesday, July 7, 2010 3:01 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:20 PM
Points: 21,751, Visits: 15,449
Excellent question



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #948866
Posted Monday, August 16, 2010 4:30 AM


Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, September 2, 2014 4:15 PM
Points: 15, Visits: 66
interesting qotd...thanks for enlighting on the difference

__________________________________________________________
Yuvaraj
Post #969668
Posted Saturday, September 18, 2010 1:42 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Today @ 8:47 AM
Points: 8,832, Visits: 9,389
Fun question.

Tom
Post #988777
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse