880

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mikeon
  • 10,499
  • 6
  • 27
  • 31

17 Answers17

748

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.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Adam Porad
  • 14,193
  • 3
  • 31
  • 56
  • 63
    while there is increased precision with datetime2, some clients doesn't support date, time, or datetime2 and force you to convert to a string literal. If you're concerned more about compatability than precision, use datetime – FistOfFury Jun 25 '14 at 20:49
  • 8
    Another option is to use an indexed view with the column converted as a datetime for compatibility. You would need to be able to point the app to the view, however. – TamusJRoyce May 08 '17 at 16:39
  • 16
    Time zone support with DATETIMEOFFSET is a misnomer. It only stores a UTC offset for a specific instant in time, not a time zone. – Suncat2000 Jun 01 '17 at 12:51
  • 6
    @Porad: What exactly is the benefit in practice of being " "more portable" due to being "SQL Standard"? That is besides making you write significantly more code that is significantly less readable / maintainable for a "port" to another RDBMS that is likely never to occur for the life of that code. Other than perhaps Microsoft-provided SQL Server tools and Drivers (if even), are there any apps that actually rely the specific Bit-level representations of the `DateTime2` Type (or any other SQL Server Type for that matter)? See the Cons in my 7/10/17 Answer below for why I'm asking. – Tom Jul 10 '17 at 23:27
  • 2
    @Adam Porad: Also, all those benefits are likely unneeded (outside of engineering or scientific apps) and therefore not worth the loss of benefits much, MUCH more likely needed: the much easier (even considering workarounds) ability to implicitly / explicitly convert to a floating-point numeric (# of days incl. if appl., fractional days since min date-time) value for additions, subtractions, minimums, maximums and averages. See the Cons in my 7/10/17 Answer below for details. – Tom Aug 10 '17 at 23:59
  • 2
    Who comes up with these function names? Any other decent programming language wouldn't accept a newer version where you add "2" to the name. What's next, datetime3 ? – HamsterWithPitchfork Nov 13 '18 at 13:22
  • 1
    @Tom Portability does put you at an advantage for switching vendors rather than being resigned to accepting whatever licensing cost a specific vendor decides to raise you to, it also makes switching to a new model like a cloud-based RDBMS easier. But maybe most importantly, it means you can seek help in the wider SQL community, rather than limiting yourself to the community for a given product (which can often be run by the company that makes the product, and may eventually have an interest in moderation that conflicts with your needs). – brianary Dec 02 '19 at 16:31
  • ***Heavy*** caveat on that MS recommendation: "_for new work_". I would take that to mean if you have an existing, legacy app, you'd need a pretty stark use case ("_These scientific tests fail when repeated b/c the date precision is too low!_") to swap, and I don't think that I'd mix the two -- casts galore for comparisons, I believe. Also, related/interesting: [`datetime` might wrap `datetime2` in SQL Server 2017+](https://blog.sqlauthority.com/2018/11/16/puzzle-datatime-to-datetime2-conversation-in-sql-server-2017/#comment-2036625). – ruffin Jul 12 '21 at 13:21
571

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!

M4N
  • 94,805
  • 45
  • 217
  • 260
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 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 Aug 26 '09 at 12:02
  • 174
    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 Oct 26 '10 at 18:16
  • 2
    @marc_s - Don't understand your point about preferring DATETIME2 when all else is equal. Is the flexibility to support year 1-1752 really a plus for most applications? I've never had an app that represented a year earlier than the late 1800's and it seems like it would encourage accidents like mis-coding 2011 as 11. – JohnFx Jan 17 '11 at 16:53
  • 1
    @JohnFx: I've had numerous occasions where apps would represent a "unknown" date as 01/01/0001 - which always causes trouble on SQL Server, unless you can use `DATETIME2` – marc_s Jan 17 '11 at 17:01
  • 11
    @marc_s - Isn't that what null is for? – JohnFx Jan 17 '11 at 17:15
  • 4
    @Marc: There *is* a difference in the mapping to .Net types: DateTime2 is isomorphic with the .Net DateTime type, so no 'out of range' errors and probably some really tiny performance improvement as no conversion required. – piers7 Jan 31 '11 at 00:58
  • 9
    @JohnFX - a bit late here - but you wouldn't set a datetime to null. you would use Nullable or datetime? which handles null just fine - and in mapping to a proc would simply do param.value = someDateTime?? DBValue.Null Its unfortunate we're stuck with a datatype with a number after it - just seems so 'generic' : ) – Adam Tuliper Jun 01 '11 at 18:25
  • 78
    Lol, I just tried to upvote my own comment (above), before I realized it was my own comment (made over a year ago). I'm still dealing with the .NET framework's dumb design decision to TRUNCATE all DateTime values by default when passed as SqlParameters unless you explicitly set it to the more precise SqlDbType.DateTime2. So much for automatically inferring the correct type. Really, they should have made the change transparent, replacing the less precise, less efficient, limited-range implementation, and kept the original "datetime" type name. See also http://stackoverflow.com/q/8421332/88409 – Triynko Dec 08 '11 at 18:25
  • 5
    @marc_s Isn't that what `Nullable` is for? – ChrisW Jan 06 '15 at 18:29
  • I don't find absolutely correct to say "Both types map to System.DateTime in .NET - no difference there". At least nowadays EF Core recognizes `System.DateTime` as Sql datatype `Datetime2` – Bazil Oct 20 '22 at 20:41
  • @Bazil: my response was from **2009** - yes, it probably has changed in the mean time .... – marc_s Oct 21 '22 at 03:45
250

datetime2 wins in most aspects except (old apps Compatibility)

  1. larger range of values
  2. better Accuracy
  3. smaller storage space (if optional user-specified precision is specified)

SQL Date and time data types compare - datetime,datetime2,date,TIME

please note the following points

  • Syntax
    • datetime2[(fractional seconds precision=> Look Below Storage Size)]
  • Precision, scale
    • 0 to 7 digits, with an accuracy of 100ns.
    • The default precision is 7 digits.
  • Storage Size
    • 6 bytes for precision less than 3;
    • 7 bytes for precision 3 and 4.
    • All other precision require 8 bytes.
  • DateTime2(3) have the same number of digits as DateTime but uses 7 bytes of storage instead of 8 byte (SQLHINTS- DateTime Vs DateTime2)
  • Find more on datetime2(Transact-SQL MSDN article)

image source : MCTS Self-Paced Training Kit (Exam 70-432): Microsoft® SQL Server® 2008 - Implementation and Maintenance Chapter 3:Tables -> Lesson 1: Creating Tables -> page 66

Iman
  • 17,932
  • 6
  • 80
  • 90
  • 8
    Thanks for showing that statistics +1 for it, `datetime2` is awesome(Winner) – Pankaj Parkar Mar 25 '16 at 11:54
  • 2
    @Iman Abidi: According to Oskar Berggren's comment dated September 10, 2014 at 3:51 pm on the "SQLHINTS- DateTime Vs DateTime2" article you referenced: "datetime2(3) is NOT the same as datetime. They will have the same number of digits, but the precision of datetime is 3.33ms, while the precision of datetime2(3) is 1ms." – Tom Jul 10 '17 at 23:41
  • 1
    @PankajParkar: Woah, not so fast. You might want to look at the Cons section of my Answer dated 7/10/17 below. – Tom Jul 10 '17 at 23:46
  • How does `datetime2` use less storage space than `datetime` and yet offer a larger range and higher precision? – Dai Mar 27 '19 at 09:28
  • One thing I ran into is that taking the MAX() of a DATETIME2(7) results in an INT. So if you compare the original value with the MAX of that column for a group of rows, you only get precision to the minute in the comparison and don't get the expected results when trying to find the latest entry in a group. That was...unexpected. – Mark Freeman Nov 12 '20 at 19:48
  • 1
    @Dai pretty sure the answer explains this. If you use `datetime2(3)` you save space for an analog for `datetime`. If you declare a higher precision (not an option in `datetime`), you don't save space, but you do gain precision, natch. **TL;DR** The space savings is for equal precision. – ruffin Jul 12 '21 at 13:24
117

I concurr with @marc_s and @Adam_Poward -- DateTime2 is the preferred method moving forward. It has a wider range of dates, higher precision, and uses equal or less storage (depending on precision).

One thing the discussion missed, however...
@Marc_s states: Both types map to System.DateTime in .NET - no difference there. This is correct, however, the inverse is not true...and it matters when doing date range searches (e.g. "find me all records modified on 5/5/2010").

.NET's version of Datetime has similar range and precision to DateTime2. When mapping a .net Datetime down to the old SQL DateTime an implicit rounding occurs. The old SQL DateTime is accurate to 3 milliseconds. This means that 11:59:59.997 is as close as you can get to the end of the day. Anything higher is rounded up to the following day.

Try this :

declare @d1 datetime   = '5/5/2010 23:59:59.999'
declare @d2 datetime2  = '5/5/2010 23:59:59.999'
declare @d3 datetime   = '5/5/2010 23:59:59.997'
select @d1 as 'IAmMay6BecauseOfRounding', @d2 'May5', @d3 'StillMay5Because2msEarlier'

Avoiding this implicit rounding is a significant reason to move to DateTime2. Implicit rounding of dates clearly causes confusion:

Community
  • 1
  • 1
EBarr
  • 11,826
  • 7
  • 63
  • 85
  • 17
    You can also avoid this rounding by not trying to find the "end" of a day anyway. >= May 5 AND < May 6 is much safer and will work on any of the date/time types (except TIME of course). Also suggest avoiding regional, ambiguous formats like m/d/yyyy. – Aaron Bertrand Feb 11 '14 at 19:55
  • 2
    @AaronBertrand - totally agree, but looking at the number of questions we have the matter it seemed worth describing. – EBarr Feb 13 '14 at 19:54
  • 2
    Why did you switch from `20100505` to `5/5/2010`? The former format will work with any region in SQL Server. The latter will break: `SET LANGUAGE French; SELECT Convert(datetime, '1/7/2015')` oops: `2015-07-01 00:00:00.000` – ErikE Sep 23 '15 at 22:35
  • 1
    @EBarr: Re. "DateTime2 is the preferred method moving forward. It has a wider range of dates, higher precision, and uses equal or less storage (depending on precision": I strongly disagree. See the Cons section of my Answer dated 7/10/17 below. In short, those benefits are likely unneeded (outside engineering/scientific apps) and therefore not worth the loss of benefits MUCH more likely needed, the much easier (even considering workarounds) ability to implicitly / explicitly convert to a floating-point numeric (# of days incl. if appl., fractions since min date-time) value for +, - and avg. – Tom Aug 10 '17 at 23:52
30

Almost all the Answers and Comments have been heavy on the Pros and light on the Cons. Here's a recap of all Pros and Cons so far plus some crucial Cons (in #2 below) I've only seen mentioned once or not at all.

  1. PROS:

1.1. More ISO compliant (ISO 8601) (although I don’t know how this comes into play in practice).

1.2. More range (1/1/0001 to 12/31/9999 vs. 1/1/1753-12/31/9999) (although the extra range, all prior to year 1753, will likely not be used except for ex., in historical, astronomical, geologic, etc. apps).

1.3. Exactly matches the range of .NET’s DateTime Type’s range (although both convert back and forth with no special coding if values are within the target type’s range and precision except for Con # 2.1 below else error / rounding will occur).

1.4. More precision (100 nanosecond aka 0.000,000,1 sec. vs. 3.33 millisecond aka 0.003,33 sec.) (although the extra precision will likely not be used except for ex., in engineering / scientific apps).

1.5. When configured for similar (as in 1 millisec not "same" (as in 3.33 millisec) as Iman Abidi has claimed) precision as DateTime, uses less space (7 vs. 8 bytes), but then of course, you’d be losing the precision benefit which is likely one of the two (the other being range) most touted albeit likely unneeded benefits).

  1. CONS:

2.1. When passing a Parameter to a .NET SqlCommand, you must specify System.Data.SqlDbType.DateTime2 if you may be passing a value outside the SQL Server DateTime’s range and/or precision, because it defaults to System.Data.SqlDbType.DateTime.

2.2. Cannot be implicitly / easily converted to a floating-point numeric (# of days since min date-time) value to do the following to / with it in SQL Server expressions using numeric values and operators:

2.2.1. add or subtract # of days or partial days. Note: Using DateAdd Function as a workaround is not trivial when you're needing to consider multiple if not all parts of the date-time.

2.2.2. take the difference between two date-times for purposes of “age” calculation. Note: You cannot simply use SQL Server’s DateDiff Function instead, because it does not compute age as most people would expect in that if the two date-times happens to cross a calendar / clock date-time boundary of the units specified if even for a tiny fraction of that unit, it’ll return the difference as 1 of that unit vs. 0. For example, the DateDiff in Day’s of two date-times only 1 millisecond apart will return 1 vs. 0 (days) if those date-times are on different calendar days (i.e. “1999-12-31 23:59:59.9999999” and “2000-01-01 00:00:00.0000000”). The same 1 millisecond difference date-times if moved so that they don’t cross a calendar day, will return a “DateDiff” in Day’s of 0 (days).

2.2.3. take the Avg of date-times (in an Aggregate Query) by simply converting to “Float” first and then back again to DateTime.

NOTE: To convert DateTime2 to a numeric, you have to do something like the following formula which still assumes your values are not less than the year 1970 (which means you’re losing all of the extra range plus another 217 years. Note: You may not be able to simply adjust the formula to allow for extra range because you may run into numeric overflow issues.

25567 + (DATEDIFF(SECOND, {d '1970-01-01'}, @Time) + DATEPART(nanosecond, @Time) / 1.0E + 9) / 86400.0 – Source: “ https://siderite.dev/blog/how-to-translate-t-sql-datetime2-to.html

Of course, you could also Cast to DateTime first (and if necessary back again to DateTime2), but you'd lose the precision and range (all prior to year 1753) benefits of DateTime2 vs. DateTime which are prolly the 2 biggest and also at the same time prolly the 2 least likely needed which begs the question why use it when you lose the implicit / easy conversions to floating-point numeric (# of days) for addition / subtraction / "age" (vs. DateDiff) / Avg calcs benefit which is a big one in my experience.

Btw, the Avg of date-times is (or at least should be) an important use case. a) Besides use in getting average duration when date-times (since a common base date-time) are used to represent duration (a common practice), b) it’s also useful to get a dashboard-type statistic on what the average date-time is in the date-time column of a range / group of Rows. c) A standard (or at least should be standard) ad-hoc Query to monitor / troubleshoot values in a Column that may not be valid ever / any longer and / or may need to be deprecated is to list for each value the occurrence count and (if available) the Min, Avg and Max date-time stamps associated with that value.

Siderite Zackwehdex
  • 6,293
  • 3
  • 30
  • 46
Tom
  • 870
  • 11
  • 13
  • 1
    Like the contrarian view - it points out the c# side of the equation. Combined with all the other "pros" it will allow people to make a good choice based on where they want to take their pain. – EBarr Aug 11 '17 at 15:47
  • 1
    @EBarr: Only the Cons #1 part of my "'contrarian view'" "points out the c# side of the equation". The rest (Cons #'s 2.2.1 - 2.2.3), which like I said are the far more likely needed benefits (of `DateTime`), are all related to effects on SQL Server Queries and statements. – Tom Aug 12 '17 at 01:59
  • Re 2.2.1 -- it is considered an unsafe practice to do arithmetic on dates, and the preferred way is always to use DateAdd and related functions. This is best practice. There are serious liabilities to doing date arithmetic, not the least of which is it doesn't work for most date types. A few articles: https://www.sqlservercentral.com/blogs/manipulating-dates-and-times-in-t-sql https://sqlblog.org/2011/09/20/bad-habits-to-kick-using-shorthand-with-date-time-operations – RBerman Aug 14 '19 at 15:06
  • @RBerman: Re. "unsafe": It's only unsafe with certain date types (like the `DateTime2` I already mentioned (due to high chance of overflow)). Re. "desn't work for most date types": You only need it to work with one, and most dates in most apps will likely never need to be converted to another date type for their entire life times (except maybe, like I also mentioned, `DateTime2` to `DateTime` (e.g. to do "arithmetic on dates" ;P). Given that, it's not worth all the extra coding in not only programmed but also ad-hoc research queries to use a non-arithmetic friendly date type. – Tom Dec 11 '19 at 18:20
18

Here is an example that will show you the differences in storage size (bytes) and precision between smalldatetime, datetime, datetime2(0), and datetime2(7):

DECLARE @temp TABLE (
    sdt smalldatetime,
    dt datetime,
    dt20 datetime2(0),
    dt27 datetime2(7)
)

INSERT @temp
SELECT getdate(),getdate(),getdate(),getdate()

SELECT sdt,DATALENGTH(sdt) as sdt_bytes,
    dt,DATALENGTH(dt) as dt_bytes,
    dt20,DATALENGTH(dt20) as dt20_bytes,
    dt27, DATALENGTH(dt27) as dt27_bytes FROM @temp

which returns

sdt                  sdt_bytes  dt                       dt_bytes  dt20                 dt20_bytes  dt27                         dt27_bytes
-------------------  ---------  -----------------------  --------  -------------------  ----------  ---------------------------  ----------
2015-09-11 11:26:00  4          2015-09-11 11:25:42.417  8         2015-09-11 11:25:42  6           2015-09-11 11:25:42.4170000  8

So if I want to store information down to the second - but not to the millisecond - I can save 2 bytes each if I use datetime2(0) instead of datetime or datetime2(7).

Adam Porad
  • 14,193
  • 3
  • 31
  • 56
Baodad
  • 2,405
  • 2
  • 38
  • 39
15

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.

Rhett A Brown
  • 169
  • 1
  • 2
12

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:

WHERE ENTRY_TIME >= '2017-01-01 00:00:00' AND ENTRY_TIME < '2018-01-01 00:00:00'

The query was fine initially when there were hundreds of rows, but when number of rows increased, the query started to give this error:

Execution Timeout Expired. The timeout period elapsed prior
to completion of the operation or the server is not responding.

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

MJ Khan
  • 1,696
  • 3
  • 21
  • 36
11

Interpretation of date strings into datetime and datetime2 can be different too, when using non-US DATEFORMAT settings. E.g.

set dateformat dmy
declare @d datetime, @d2 datetime2
select @d = '2013-06-05', @d2 = '2013-06-05'
select @d, @d2

This returns 2013-05-06 (i.e. May 6) for datetime, and 2013-06-05 (i.e. June 5) for datetime2. However, with dateformat set to mdy, both @d and @d2 return 2013-06-05.

The datetime behavior seems at odds with the MSDN documentation of SET 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.

Richard Fawcett
  • 2,799
  • 1
  • 29
  • 36
  • 3
    Nope. For ISO 8601 I think you meant YYYYMMDD (no dashes). `SET LANGUAGE FRENCH; DECLARE @d DATETIME = '20130605'; SELECT @d;` Try again with the dashes. – Aaron Bertrand Feb 11 '14 at 19:57
  • 1
    The standard allows for both YYYY-MM-DD and YYYYMMDD formats for calendar date representations. I think MSDN should be more specific about which subset of the ISO 8601 specification is interpreted independently! – Richard Fawcett Feb 11 '14 at 22:07
  • 2
    I know that but in SQL Server only the no-dash syntax is safe. – Aaron Bertrand Feb 11 '14 at 22:17
10

while there is increased precision with datetime2, some clients doesn't support date, time, or datetime2 and force you to convert to a string literal. Specifically Microsoft mentions "down level" ODBC, OLE DB, JDBC, and SqlClient issues with these data types and has a chart showing how each can map the type.

If value compatability over precision, use datetime

FistOfFury
  • 6,735
  • 7
  • 49
  • 57
6

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.

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
jKlaus
  • 382
  • 3
  • 8
4

I just stumbled across one more advantage for DATETIME2: it avoids a bug in the Python adodbapi module, which blows up if a standard library datetime value is passed which has non-zero microseconds for a DATETIME column but works fine if the column is defined as DATETIME2.

Bob Kline
  • 304
  • 1
  • 14
2

As the other answers show datetime2 is recommended due to smaller size and more precision, but here are some thoughts on why NOT to use datetime2 from Nikola Ilic:

  • lack of (simple) possibility to do basic math operations with dates, like GETDATE()+1
  • every time you are doing comparisons with DATEADD or DATEDIFF, you will finish with implicit data conversion to datetime
  • SQL Server can’t use statistics properly for Datetime2 columns, due to a way data is stored that leads to non-optimal query plans, which decrease the performance
robotik
  • 1,837
  • 1
  • 20
  • 26
  • > "SQL Server can’t use statistics properly for Datetime2 columns, due to a way data is stored that leads to non-optimal query plans, which decrease the performance" Citation needed – Milney Jan 21 '22 at 19:32
  • @Milney it is quoted from the article mentioned (3rd paragraph from the end) - https://towardsdatascience.com/datetime2-why-you-should-not-use-it-70e50ae2bab9 – robotik Jan 24 '22 at 11:56
0

I think DATETIME2 is the better way to store the date, because it has more efficiency than the DATETIME. In SQL Server 2008 you can use DATETIME2, it stores a date and time, takes 6-8 bytes to store and has a precision of 100 nanoseconds. So anyone who needs greater time precision will want DATETIME2.

Muhammad Waheed
  • 1,048
  • 1
  • 13
  • 30
James
  • 172
  • 2
  • 10
0

Accepted answer is great, just know that if you are sending a DateTime2 to the frontend - it gets rounded to the normal DateTime equivalent.

This caused a problem for me because in a solution of mine I had to compare what was sent with what was on the database when resubmitted, and my simple comparison '==' didn't allow for rounding. So it had to be added.

-1

datetime2 is better

  • datetime range : 1753-01-01 through 9999-12-31 , datetime2 range : 0001-01-01 through 9999-12-31

  • datetime Accuracy : 0.00333 second , datetime2 Accuracy : 100 nanoseconds

  • datetime get 8 bytes , datetime2 get 6 to 8 bytes depends on precisions

    (6 bytes for precision less than 3 , 7 bytes for precision 3 or 4 , All other precision require 8 bytes, Click and Look at the below picture)

enter image description here

M Komaei
  • 7,006
  • 2
  • 28
  • 34
-3
Select ValidUntil + 1
from Documents

The above SQL won't work with a DateTime2 field. It returns and error "Operand type clash: datetime2 is incompatible with int"

Adding 1 to get the next day is something developers have been doing with dates for years. Now Microsoft have a super new datetime2 field that cannot handle this simple functionality.

"Let's use this new type that is worse than the old one", I don't think so!

Paul McCarthy
  • 818
  • 9
  • 24
  • 2
    Just so we're clear here the `datetime` and `datetime2` data types were both introduced in SQL Server 2008. You also get `Operand type clash: date is incompatible with int` from the `date` type which has been around since day dot. All three data types work just fine with `dateadd(dd, 1, ...)` though. – AlwaysLearning Aug 21 '17 at 04:35
  • 2
    This is not clear. I have a SQLServer 2005 database with a datetime field in it. – Paul McCarthy Aug 21 '17 at 08:35