Which one:
is the recommended way to store date and time in SQL Server 2008+?
I'm aware of differences in precision (and storage space probably), but ignoring those for now, is there a best practice document on when to use what, or maybe we should just use datetime2
only?
I just stumbled across one more advantage for
DATETIME2
: it avoids a bug in the Pythonadodbapi
module, which blows up if a standard librarydatetime
value is passed which has non-zero microseconds for aDATETIME
column but works fine if the column is defined asDATETIME2
.Old Question... But I want to add something not already stated by anyone here... (Note: This is my own observation, so don't ask for any reference)
Datetime2 is faster when used in filter criteria.
TLDR:
In SQL 2016 I had a table with hundred thousand rows and a datetime column ENTRY_TIME because it was required to store the exact time up to seconds. While executing a complex query with many joins and a sub query, when I used where clause as:
The query was fine initially when there were hundreds of rows, but when number of rows increased, the query started to give this error:
I removed the where clause, and unexpectedly, the query was run in 1 sec, although now ALL rows for all dates were fetched. I run the inner query with where clause, and it took 85 seconds, and without where clause it took 0.01 secs.
I came across many threads here for this issue as datetime filtering performance
I optimized query a bit. But the real speed I got was by changing the datetime column to datetime2.
Now the same query that timed out previously takes less than a second.
cheers
The MSDN documentation for datetime recommends using datetime2. Here is their recommendation:
datetime2 has larger date range, a larger default fractional precision, and optional user-specified precision. Also depending on the user-specified precision it may use less storage.
DateTime2 wreaks havoc if you are an Access developer trying to write Now() to the field in question. Just did an Access -> SQL 2008 R2 migration and it put all the datetime fields in as DateTime2. Appending a record with Now() as the value bombed out. It was okay on 1/1/2012 2:53:04 PM, but not on 1/10/2012 2:53:04 PM.
Once character made the difference. Hope it helps somebody.
According to this article, if you would like to have the same precision of DateTime using DateTime2 you simply have to use DateTime2(3). This should give you the same precision, take up one fewer bytes, and provide an expanded range.
Interpretation of date strings into
datetime
anddatetime2
can be different too, when using non-USDATEFORMAT
settings. E.g.This returns
2013-05-06
(i.e. May 6) fordatetime
, and2013-06-05
(i.e. June 5) fordatetime2
. However, withdateformat
set tomdy
, both@d
and@d2
return2013-06-05
.The
datetime
behavior seems at odds with the MSDN documentation ofSET DATEFORMAT
which states: Some character strings formats, for example ISO 8601, are interpreted independently of the DATEFORMAT setting. Obviously not true!Until I was bitten by this, I'd always thought that
yyyy-mm-dd
dates would just be handled right, regardless of the language / locale settings.