views:

4210

answers:

3

Which one:

  • datetime
  • datetime2

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?

+1  A: 

Use datetime if do not need such precision as datetime2. Using datetime2 always is like using int32 instead of byte.

IPD: and datetime had larger year diapason

x2
I remember seeing once that using Int32 instead of Byte may actaully perform better - with explaination being that we live in 32 bit world... Don't really remember the details.
Mikeon
Actually this is not correct. Even though datetime2 has more precision, the storage size is less. datatime2 allows the user to specify the precision and uses 6 bytes for precisions less than 3; 7 bytes for precisions 3 and 4. All other precisions require 8 bytes. datetime always uses 8 bytes.
Adam Porad
+13  A: 

DATETIME2 has a date range of "0001 / 01 / 01" through "9999 / 12 / 31" while the DATETIME type only supports year 1753-9999.

Also, if you need to, DATETIME2 can be more precise in terms of time; DATETIME is limited to 3 1/3 milliseconds, while DATETIME2 can be accurate down to 100ns.

Both types map to System.DateTime in .NET - no difference there.

If you have the choice, I would recommend using DATETIME2 whenever possible. I don't see any benefits using DATETIME (except for backward compatibility) - you'll have less trouble (with dates being out of range and hassle like that).

Plus: if you only need the date (without time part), use DATE - it's just as good as DATETIME2 and saves you space, too! :-) Same goes for time only - use TIME. That's what these types are there for!

Marc

marc_s
I would agree with Marc. Unless you need backwards compatibility for some reason, always go with DATETIME2. Storage space is cheap and according to what I've seen in my own experience and read elsewhere, there is no difference in processing overhead between the two.
BBlake
Be careful when adding a .NET DateTime value as a parameter to an SqlCommand, because it likes to assume it's the old datetime type, and you'll get an error if you try to write a DateTime value that's outside that 1753-9999 year range unless you explicitly specify the type as System.Data.SqlDbType.DateTime2 for the SqlParameter. Anyway, datetime2 is great, because it can store any value that can be stored in the .NET DateTime type.
Triynko
+4  A: 

The MSDN documentation for datetime recommends using datetime2. Here is their recommendation:

Use the time, date, datetime2 and datetimeoffset data types for new work. These types align with the SQL Standard. They are more portable. time, datetime2 and datetimeoffset provide more seconds precision. datetimeoffset provides time zone support for globally deployed applications.

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.

Adam Porad

related questions