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


DateTime Precision


DateTime Precision

Author
Message
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)

Group: General Forum Members
Points: 146520 Visits: 13350
Bruce.sexton (11/28/2011)
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


Check out the previous comments, they have all the explanation you need.


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
Dwayne Dibley
Dwayne Dibley
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: 1308 Visits: 3171
Oops - I got it wrongBlush
Britt Cluff
Britt Cluff
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3309 Visits: 253
Good question. I have ran into issues with BETWEEN on several occasions.

http://brittcluff.blogspot.com/
Mike Is Here
Mike Is Here
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2424 Visits: 517
declare @date datetime

Set @date = '2011-07-31 00:00:00.000'

IF (@date BETWEEN '2011-07-01' and '2011-07-31')
PRINT 'Yes'
ELSE
PRINT 'No'

does return Yes
Daniel Bowlin
Daniel Bowlin
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17538 Visits: 2629
vk-kirov (11/27/2011)
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'.


Thanks for this. The question and the answer bothered me, but once I realized I was doing a CAST/CONVERT in my head (that wasn't there) everything made sense.:-)
roger.plowman
roger.plowman
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4616 Visits: 1565
You know, the fact this was a trick question highlights a glaring flaw in T/SQL (or perhaps SQL in general?). Using single quotes for both strings and dates is a brain-dead design decision--period.

Would it be so hard to introduce a new quote character for dates, ala Access?

For example:

#02/21/2008# or #12/31/1899 14:23#

Ambiguity is a nasty design flaw, SQL Server (or SQL in general?) is far enough down the road this should have been fixed by now...
cengland0
cengland0
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3324 Visits: 1300
roger.plowman (11/28/2011)
You know, the fact this was a trick question highlights a glaring flaw in T/SQL (or perhaps SQL in general?). Using single quotes for both strings and dates is a brain-dead design decision--period.

Would it be so hard to introduce a new quote character for dates, ala Access?

For example:

#02/21/2008# or #12/31/1899 14:23#

Ambiguity is a nasty design flaw, SQL Server (or SQL in general?) is far enough down the road this should have been fixed by now...

I write SQL for both MSSQL servers and Access. I get those dates mixed up all the time when I'm programming for Access. I keep forgetting to put those #'s instead of single quotes. I do wish they were the same.
Koen Verbeeck
Koen Verbeeck
SSC Guru
SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)SSC Guru (146K reputation)

Group: General Forum Members
Points: 146520 Visits: 13350
roger.plowman (11/28/2011)
You know, the fact this was a trick question highlights a glaring flaw in T/SQL (or perhaps SQL in general?). Using single quotes for both strings and dates is a brain-dead design decision--period.

Would it be so hard to introduce a new quote character for dates, ala Access?

For example:

#02/21/2008# or #12/31/1899 14:23#

Ambiguity is a nasty design flaw, SQL Server (or SQL in general?) is far enough down the road this should have been fixed by now...


That's a good suggestion. It would get rid of some nasty accidental implicit conversion bugs.


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
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: 34518 Visits: 13121
roger.plowman (11/28/2011)
Using single quotes for both strings and dates is a brain-dead design decision--period.

Technically, T-SQL is not using single quotes for both strings and dates. It uses single quotes for strings only.
There is no way to have date literals in T-SQL; that's why we have to make do with string literals that will be converted (at execution time) to date or datetime values.

</pedantic>


Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Steve Jones
Steve Jones
SSC Guru
SSC Guru (331K reputation)SSC Guru (331K reputation)SSC Guru (331K reputation)SSC Guru (331K reputation)SSC Guru (331K reputation)SSC Guru (331K reputation)SSC Guru (331K reputation)SSC Guru (331K reputation)

Group: Administrators
Points: 331103 Visits: 20112
My apologies. I have corrected the explanation to note the string comparison.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
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