November 26, 2011 at 5:04 pm
Comments posted to this topic are about the item DateTime Precision
November 26, 2011 at 5:08 pm
November 27, 2011 at 7:07 am
Good observation. Thanks.
M&M
November 27, 2011 at 6:49 pm
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.
November 27, 2011 at 9:33 pm
cengland0 (11/27/2011)
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.
Heck no one is perfect, and if memory serves me correctly those who write the explanations for BOL are NOT the developers or actual personnel writing the code ...
November 27, 2011 at 9:35 pm
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'
November 27, 2011 at 9:41 pm
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'.
November 27, 2011 at 10:41 pm
Glad I checked out the comments after getting this one wrong. I hate learning things that aren't true. 🙂
-- Stephen Cook
November 27, 2011 at 11:31 pm
Silly trick question...
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
November 28, 2011 at 12:46 am
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
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
November 28, 2011 at 1:02 am
It is a string comparation.
Dates comparation might be:
IF (CAST( '2011-07-31 00:00:00.000' AS DATETIME) BETWEEN CAST( '2011-07-01' AS DATETIME) and CAST( '2011-07-31' AS DATETIME) )
PRINT 'Yes'
ELSE
PRINT 'No'
Regards,
Iulian
November 28, 2011 at 1:43 am
Hi jkelly - tricky question!
November 28, 2011 at 2:22 am
Thanks for the question and also to the posts gave me something to think about on a cold Monday morning.
November 28, 2011 at 2:30 am
As several people have pointed out, this "explanation" is bogus.
SQL Server is comparing these values as strings - this has nothing to do with datetime precision. Replace the '2011-07-31 00:00:00.000' with '2011-07-31 REDHERRING' and you get the same result.
November 28, 2011 at 2:31 am
I overlooked that it is a STRING comparison and got the answer wrong.:-) (though writer had intention to show datetime comparison)
I updated the code as follows and executed:
DECLARE @DATE DATETIME
SET @DATE = '2011-07-31 00:00:00.001'
IF (@DATE BETWEEN '2011-07-01' and '2011-07-31')
PRINT 'Yes'
ELSE
PRINT 'No'
The output came 'Yes'. However when I changed the @date value to '2011-07-31 00:00:00.002' the result was 'No'. As per my knowledge the @date value should be compared to the end date '2011-07-31 00:00:00.000'. How '2011-07-31 00:00:00.001' be less than or equal to '2011-07-31 00:00:00.000'?:unsure:
I executed this on SQL Sever 2008 EE Ver. 10.0.4064.0
Viewing 15 posts - 1 through 15 (of 37 total)
You must be logged in to reply to this topic. Login to reply