Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Datetime precision Expand / Collapse
Author
Message
Posted Sunday, November 29, 2009 5:27 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Friday, January 3, 2014 10:59 AM
Points: 626, Visits: 836
Comments posted to this topic are about the item Datetime precision

Craig Outcalt



Tips for new DBAs: http://www.sqlservercentral.com/articles/Career/64632
My other articles: http://www.sqlservercentral.com/Authors/Articles/Craig_Outcalt/560258
Post #825871
Posted Monday, November 30, 2009 3:15 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, October 17, 2014 8:46 AM
Points: 280, Visits: 90
I will personaly prefer to do something like cast(cast(getdate() as int) as datetime) to obtain de date of the day at midnight rather using datediff+dateadd functions.


My MCP Transcript (ID : 692471 Access : 109741229)
Post #826087
Posted Monday, November 30, 2009 5:52 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 10:16 AM
Points: 2,331, Visits: 566
The wording of this question is ambiguous - if the question is "what would you expect this query to return", then any answer you select is correct by definition, as it's what you would expect.


Post #826139
Posted Monday, November 30, 2009 6:28 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 9:36 PM
Points: 20,738, Visits: 32,522
Dude76 (11/30/2009)
I will personaly prefer to do something like cast(cast(getdate() as int) as datetime) to obtain de date of the day at midnight rather using datediff+dateadd functions.


There has been several discussions regarding this and iirc it was actually proven that the dateadd/datediff method is actually faster. Also, it doesn't use the knowledge of how datetime values are stored. If MS changes this, the dateadd/datediff method should still work as advertised.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #826151
Posted Monday, November 30, 2009 6:34 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 1:00 AM
Points: 2,563, Visits: 2,420
SQLBOT (11/29/2009)
Comments posted to this topic are about the item <A HREF="/questions/Datetime+Manipulation/68075/">Datetime precision</A>

Thanks. After year and years working with sql, there always is something to learn.
Post #826153
Posted Monday, November 30, 2009 7:07 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, April 5, 2011 8:15 AM
Points: 137, Visits: 29
While looking at the answer choices, my brain kept saying "Yesterday Midnight + 100ms"

For me, Midnight today is tonight and the SQL answer is this morning at 100ms after midnight or 100ms after the beginning of day.

Post #826177
Posted Monday, November 30, 2009 7:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, October 6, 2010 7:54 AM
Points: 126, Visits: 75
I'm confused, if DateTime is only precise to 3.33 ms, why then is the answer just 1 ms off? Why not 3 ms?
Post #826185
Posted Monday, November 30, 2009 7:21 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, October 17, 2014 8:46 AM
Points: 280, Visits: 90
Lynn Pettis (11/30/2009)
Dude76 (11/30/2009)
I will personaly prefer to do something like cast(cast(getdate() as int) as datetime) to obtain de date of the day at midnight rather using datediff+dateadd functions.


There has been several discussions regarding this and iirc it was actually proven that the dateadd/datediff method is actually faster. Also, it doesn't use the knowledge of how datetime values are stored. If MS changes this, the dateadd/datediff method should still work as advertised.

Oh, faster rather cast, really ?
I didn't know that, and were sure that cast were a fast operation front of manipulating functions.
Thx for the info, i'll test if to have an idea of the difference of performance.



My MCP Transcript (ID : 692471 Access : 109741229)
Post #826188
Posted Monday, November 30, 2009 8:23 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Wednesday, October 22, 2014 12:34 PM
Points: 31,181, Visits: 15,626
the dateadd is not commented out, just the note.

SQL is only precise to .0, .3, and .7ms, therefore you can't have .1 as the answer. It could only be one of those 3







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #826237
Posted Monday, November 30, 2009 8:37 AM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Friday, January 3, 2014 10:59 AM
Points: 626, Visits: 836
jim.taylor (11/30/2009)
I'm confused, if DateTime is only precise to 3.33 ms, why then is the answer just 1 ms off? Why not 3 ms?


SQL Server rounded down to 100 ms from the 101ms that was added.
if you add 102ms it rounds up to 103 ms.

I posted this question because the inaccuracy caught me off guard a couple months ago when I kept getting unexpected results out of some code.

After suspecting sheer lunacy to be the root cause, I finally got the notion that maybe the SQL Server clock was dodgy. I wrote the query that you see in the question to test that theory and the BOL confirmed the rest.

To avoid the issue, use datetime2 which is more precise, but still has the same problem when you get down to 100 nanoseconds.

My issue was resolved by ignoring milliseconds since I didn't need to be that precise, and accuracy was more important than precision.

Thanks All!

~Craig








Craig Outcalt



Tips for new DBAs: http://www.sqlservercentral.com/articles/Career/64632
My other articles: http://www.sqlservercentral.com/Authors/Articles/Craig_Outcalt/560258
Post #826252
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse