SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DateTime Precision


DateTime Precision

Author
Message
Iulian -207023
Iulian -207023
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4319 Visits: 1248
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
paul s-306273
paul s-306273
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5547 Visits: 1269
Hi jkelly - tricky question!
skanker
skanker
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1849 Visits: 1385
Thanks for the question and also to the posts gave me something to think about on a cold Monday morning.
Paul Urban
Paul Urban
SSC Rookie
SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)SSC Rookie (26 reputation)

Group: General Forum Members
Points: 26 Visits: 28
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.
Sunil Chandurkar
Sunil Chandurkar
Mr or Mrs. 500
Mr or Mrs. 500 (525 reputation)Mr or Mrs. 500 (525 reputation)Mr or Mrs. 500 (525 reputation)Mr or Mrs. 500 (525 reputation)Mr or Mrs. 500 (525 reputation)Mr or Mrs. 500 (525 reputation)Mr or Mrs. 500 (525 reputation)Mr or Mrs. 500 (525 reputation)

Group: General Forum Members
Points: 525 Visits: 81
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
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)

Group: General Forum Members
Points: 144642 Visits: 13349
Sunil Chandurkar (11/28/2011)
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


This is because .001 is rounded to .000, giving you the Yes result.
.002 is rounded to .003, giving you the No result. The datatime datatype is only accurate to every 3 milliseconds. This was in a QoTD a few days ago :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Tom Thomson
Tom Thomson
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50332 Visits: 13149
Disgraceful trick question - it's not about the topic it claims to be about.

However, the utterly wrong explanation suggests that maybe the author didn't even realise that, so maybe the use of "Datetime" in the topic was not an intentional deception.

Tom

Hugo Kornelis
Hugo Kornelis
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34153 Visits: 13105
Koen Verbeeck (11/27/2011)
Silly trick question...

It would be a silly trick question if the author intended it to trick us.
The "explanation" suggests that the question author fell for the trap himself. He (or she?) simply didn't realize that string comparison would be used, then went on to think up a different explanation - one that obviously contradicts BOL. Laugh

It would have been a nice (not silly!) and great trick question if the explanation had read: "SQL Server will use string comparison, d'oh!"


Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Bruce.sexton
Bruce.sexton
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 8
So what is the correct query in your origninal question to correctly compare the datetime field with the 2 date fields? In other words how does one truncate the time portion from the datetime field to do the comparison?

Bruce
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)

Group: General Forum Members
Points: 144642 Visits: 13349
Hugo Kornelis (11/28/2011)
Koen Verbeeck (11/27/2011)
Silly trick question...

It would be a silly trick question if the author intended it to trick us.
The "explanation" suggests that the question author fell for the trap himself. He (or she?) simply didn't realize that string comparison would be used, then went on to think up a different explanation - one that obviously contradicts BOL. Laugh

It would have been a nice (not silly!) and great trick question if the explanation had read: "SQL Server will use string comparison, d'oh!"


I've realized that by now. I wasn't really awake this morning when I wrote my reply. :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search