|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 5:04 PM
Points: 338,
Visits: 188
|
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:40 PM
Points: 5,123,
Visits: 20,370
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 10:29 PM
Points: 2,178,
Visits: 3,599
|
|
Good observation. Thanks.
Mohammed Moinudheen
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354,
Visits: 1,299
|
|
Don't you hate it when the explanation says, "Despite what BOL says..."
It should either be mentioned in BOL or do what the BOL says.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:40 PM
Points: 5,123,
Visits: 20,370
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, December 09, 2012 2:25 PM
Points: 1,555,
Visits: 291
|
|
The question is not really about datetime precision, but string comparison.
If the comparison value had been declared as datetime, then the other values would be implicitly converted to datetime as well and the statement would return Yes.
declare @d as datetime set @d = '2011-07-31 00:00:00.000' IF @d BETWEEN '2011-07-01' and '2011-07-31' PRINT 'Yes' ELSE PRINT 'No'
The question as it stands could just as well have been this
if ('abcd' between 'ab' and 'abc') print 'yes' else print 'no'
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Thursday, June 13, 2013 8:50 PM
Points: 3,208,
Visits: 4,178
|
|
Nice tricky question with stupid 'explanation'. Of course, the string (not datetime) '2011-07-31 00:00:00.000' is greater than the other string '2011-07-31', and thus the result of the batch is 'No'.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, June 14, 2013 11:56 AM
Points: 859,
Visits: 765
|
|
Glad I checked out the comments after getting this one wrong. I hate learning things that aren't true.
-- Stephen Cook
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 6:30 AM
Points: 9,410,
Visits: 6,495
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 5:31 AM
Points: 2,226,
Visits: 438
|
|
Jostein Saethern (11/27/2011) The question is not really about datetime precision, but string comparison.
If the comparison value had been declared as datetime, then the other values would be implicitly converted to datetime as well and the statement would return Yes.
declare @d as datetime set @d = '2011-07-31 00:00:00.000' IF @d BETWEEN '2011-07-01' and '2011-07-31' PRINT 'Yes' ELSE PRINT 'No'
The question as it stands could just as well have been this
if ('abcd' between 'ab' and 'abc') print 'yes' else print 'no'
You are right, I'ts very obvious when you think about it, but when you see something that looks like a date, you assume that SQL server see it as a date. BUT it would be dangerous if SQL server would interpret the datatypes based on the values, and this is just another reason to dislike the implicit conversions. (I know, there is no implicit conversion in the question, but we have learned to live with them so long that we assume it takes place all the time).
I really would like a database setting, a trace flag or something that warn you or prevent you from using implicit conversion and force you to use explicit conversion. Because if you do, you know that you are doing something wrong (from a performance perspective).
The explanation is not correct and should be changed.
/Håkan Winther MCITP:Database Developer 2008
|
|
|
|