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


DateTime Precision


DateTime Precision

Author
Message
jkelly
jkelly
SSC-Addicted
SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)SSC-Addicted (447 reputation)

Group: General Forum Members
Points: 447 Visits: 211
Comments posted to this topic are about the item DateTime Precision



bitbucket-25253
bitbucket-25253
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26961 Visits: 25280
As Sherlock Holmes said to Dr. Watson ................ Very interesting

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
M&M
M&M
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9770 Visits: 3916
Good observation. Thanks.

M&M
cengland0
cengland0
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2836 Visits: 1300
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.
bitbucket-25253
bitbucket-25253
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26961 Visits: 25280
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 ...

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Jostein Saethern
Jostein Saethern
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: 1765 Visits: 319
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'
vk-kirov
vk-kirov
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5064 Visits: 4408
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'.
Stephen E. Cook
Stephen E. Cook
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1339 Visits: 818
Glad I checked out the comments after getting this one wrong. I hate learning things that aren't true. :-)

-- Stephen Cook
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (111K reputation)SSC Guru (111K reputation)SSC Guru (111K reputation)SSC Guru (111K reputation)SSC Guru (111K reputation)SSC Guru (111K reputation)SSC Guru (111K reputation)SSC Guru (111K reputation)

Group: General Forum Members
Points: 111927 Visits: 13338
Silly trick question...


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
hakan.winther
hakan.winther
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3740 Visits: 612
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
MCITPBigGrinatabase Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
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