datetime2 vs datetime in SQL Server: which one to use

datetime2 vs datetime: datetime rounds to 1/300 of a second and starts in 1753; datetime2 keeps up to 7 digits in 6 to 8 bytes. The traps, and which to use.

What is the difference between datetime2 and datetime?

Both types hold a date and a time of day, with no time zone. Microsoft describes datetime2 as an extension of datetime with a larger date range, a larger default fractional precision and an optional precision you choose. Side by side:

  • Range: datetime runs from 1753-01-01 through 9999-12-31; datetime2 from 0001-01-01 through 9999-12-31.
  • Precision: datetime rounds fractional seconds to increments of .000, .003 or .007; datetime2(n) keeps n digits, 0 to 7, down to 100 nanoseconds, and n is 7 when you leave it out.
  • Storage: datetime always takes 8 bytes; datetime2 takes 6 bytes for precision 0 to 2, 7 for 3 or 4, and 8 for 5 to 7.
  • Standards: datetime isn’t ANSI or ISO 8601 compliant, and Microsoft’s page for it says to avoid it for new work.

So datetime2(3) stores milliseconds exactly in 7 bytes, where datetime spends 8 on values it has to round. Here are three tables of an airline, cut down from the airline reservation system template and typed the way many older schemas are, with datetime everywhere:

CREATE TABLE dbo.Flight (
  FlightId     int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Flight PRIMARY KEY,
  FlightNumber varchar(8) NOT NULL,
  DepartsAt    datetime   NOT NULL,
  ArrivesAt    datetime   NOT NULL
);
CREATE TABLE dbo.Passenger (
  PassengerId int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Passenger PRIMARY KEY,
  FullName    nvarchar(200) NOT NULL,
  BirthDate   datetime      NOT NULL
);
CREATE TABLE dbo.Ticket (
  TicketId    int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Ticket PRIMARY KEY,
  FlightId    int      NOT NULL,
  PassengerId int      NOT NULL,
  IssuedAt    datetime NOT NULL
    CONSTRAINT DF_Ticket_IssuedAt DEFAULT GETDATE(),
  CONSTRAINT FK_Ticket_Flight_FlightId FOREIGN KEY (FlightId)
    REFERENCES dbo.Flight (FlightId),
  CONSTRAINT FK_Ticket_Passenger_PassengerId
    FOREIGN KEY (PassengerId)
    REFERENCES dbo.Passenger (PassengerId)
);
dbo.Flight with primary key FlightId and datetime columns DepartsAt and ArrivesAt; dbo.Passenger with primary key PassengerId and a datetime BirthDate; dbo.Ticket with primary key TicketId, foreign keys FlightId and PassengerId and a datetime IssuedAt; dashed lines from Flight and from Passenger to Ticket.
Every date and time column is datetime, the birth date too. The GETDATE() default on IssuedAt is in the script; the diagram shows the types.

How datetime rounds fractional seconds

A datetime counts the time of day in ticks of 1/300 of a second, so most millisecond values can’t be stored as written. Five tickets go in, and then a report counts the ones issued on March 14:

INSERT INTO dbo.Flight (FlightNumber, DepartsAt, ArrivesAt)
VALUES ('EA101', '2026-03-14 18:45', '2026-03-15 11:15');
INSERT INTO dbo.Passenger (FullName, BirthDate)
VALUES (N'Ana Silva', '1988-06-02');
INSERT INTO dbo.Ticket (FlightId, PassengerId, IssuedAt)
VALUES (1, 1, '2026-03-14 09:30:00.001'),
       (1, 1, '2026-03-14 09:30:00.002'),
       (1, 1, '2026-03-14 09:30:00.005'),
       (1, 1, '2026-03-14 23:59:59.999'),
       (1, 1, '2026-03-15 00:00:00');
SELECT TicketId, IssuedAt FROM dbo.Ticket;
SELECT COUNT(*) AS IssuedMarch14 FROM dbo.Ticket
WHERE IssuedAt >= '2026-03-14'
  AND IssuedAt <= '2026-03-14 23:59:59.999';

The first three come back as .000, .003 and .007 seconds. The fourth, written as 23:59:59.999, is stored as midnight on March 15. The report then counts all five, the ticket issued at midnight on the 15th included. The literal in the WHERE clause is converted to datetime as well, so the upper bound '2026-03-14 23:59:59.999' turns into 2026-03-15 00:00:00.000, and that ticket falls inside it.

Write a day as a half-open range instead, IssuedAt >= '2026-03-14' AND IssuedAt < '2026-03-15'. It is right for datetime, datetime2 and date at any precision, and it doesn’t depend on how many nines the last instant of a day has.

The range is the other limit. A date before 1753 doesn’t fit:

INSERT INTO dbo.Passenger (FullName, BirthDate)
VALUES (N'Lee Wong', '0001-01-01');
Msg 242, Level 16, State 3, Line 1
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

datetime2 and date accept any year from 1 through 9999, so the same insert into either would succeed.

Departure times in two time zones

Flight EA101 leaves Seattle at 18:45 and lands in London at 11:15 the next morning, and the airline stores both times as the airports’ clocks show them. Neither datetime nor datetime2 keeps an offset, so the database can’t tell those are two different zones:

SELECT FlightNumber,
       DATEDIFF(minute, DepartsAt, ArrivesAt) AS Minutes
FROM dbo.Flight;

It returns 990 minutes, 16 and a half hours. In mid-March Seattle is at UTC-07:00 and London at UTC+00:00, so the flight takes 570 minutes. Switching the columns to datetime2 wouldn’t help: what’s missing is the offset.

datetimeoffset stores the offset with the value. It compares, sorts and indexes values as UTC and gives back the offset they were written with, so a departure reads 18:45 -07:00 while its arithmetic is done in UTC. The offset is a fixed number of hours and minutes. The type knows no zone names and no daylight saving time; AT TIME ZONE converts a value into a named zone when you need one. The other choice is to store UTC in a datetime2 and convert for display, which works as long as every writer remembers to.

Picking the type and precision for each column

The redesign runs in a second database, AirlineDatetime2, and replaces all three tables, each column typed for what it holds, the rule SQL Server data types applies to every family:

  • DepartsAt and ArrivesAt become datetimeoffset(0): whole seconds and the airport’s offset, 8 bytes each.
  • BirthDate becomes date, 3 bytes. A birth date has no time of day, and datetime stored a midnight for it anyway.
  • IssuedAt becomes datetime2(3), 7 bytes, which orders tickets sold within the same second. A booking or departure time needs only datetime2(0), 6 bytes. Fewer digits round the rest: in a run, 09:30:00.9999999 cast to datetime2(0) became 09:30:01.

The time type, 3 to 5 bytes, holds a time of day without a date, such as a timetable’s daily 18:45. smalldatetime saves bytes only by losing more: 4 bytes, whole minutes, and years 1900 through 2079 (smalldatetime). Write the precision on every column: datetime2 with none is datetime2(7), 8 bytes, as many as datetime takes, for seven digits most columns never use. An ORM writes none either: EF Core’s SQL Server provider maps a .NET DateTime to datetime2(7) unless the property is given a precision.

CREATE TABLE dbo.Flight (
  FlightId     int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Flight PRIMARY KEY,
  FlightNumber varchar(8)        NOT NULL,
  DepartsAt    datetimeoffset(0) NOT NULL,
  ArrivesAt    datetimeoffset(0) NOT NULL,
  CONSTRAINT CK_Flight_ArrivesAt CHECK (ArrivesAt > DepartsAt)
);
CREATE TABLE dbo.Passenger (
  PassengerId int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Passenger PRIMARY KEY,
  FullName    nvarchar(200) NOT NULL,
  BirthDate   date          NOT NULL
);
CREATE TABLE dbo.Ticket (
  TicketId    int IDENTITY(1, 1) NOT NULL
    CONSTRAINT PK_Ticket PRIMARY KEY,
  FlightId    int          NOT NULL,
  PassengerId int          NOT NULL,
  IssuedAt    datetime2(3) NOT NULL
    CONSTRAINT DF_Ticket_IssuedAt DEFAULT SYSDATETIME(),
  CONSTRAINT FK_Ticket_Flight_FlightId FOREIGN KEY (FlightId)
    REFERENCES dbo.Flight (FlightId),
  CONSTRAINT FK_Ticket_Passenger_PassengerId
    FOREIGN KEY (PassengerId)
    REFERENCES dbo.Passenger (PassengerId)
);
dbo.Flight with primary key FlightId and datetimeoffset(0) columns DepartsAt and ArrivesAt; dbo.Passenger with primary key PassengerId and a date BirthDate; dbo.Ticket with primary key TicketId, foreign keys FlightId and PassengerId and a datetime2(3) IssuedAt; dashed lines from Flight and from Passenger to Ticket.
Each type carries its precision on the diagram. The CHECK that a flight arrives after it departs and the SYSDATETIME() default are in the script.

Three of the same tickets, plus one that takes its time from the default:

INSERT INTO dbo.Flight (FlightNumber, DepartsAt, ArrivesAt)
VALUES ('EA101', '2026-03-14 18:45 -07:00',
        '2026-03-15 11:15 +00:00');
INSERT INTO dbo.Passenger (FullName, BirthDate)
VALUES (N'Ana Silva', '1988-06-02');
INSERT INTO dbo.Ticket (FlightId, PassengerId, IssuedAt)
VALUES (1, 1, '2026-03-14 09:30:00.002'),
       (1, 1, '2026-03-14 23:59:59.999'),
       (1, 1, '2026-03-15 00:00:00');
INSERT INTO dbo.Ticket (FlightId, PassengerId)
VALUES (1, 1);
SELECT TicketId, IssuedAt FROM dbo.Ticket;
SELECT COUNT(*) AS IssuedMarch14 FROM dbo.Ticket
WHERE IssuedAt >= '2026-03-14' AND IssuedAt < '2026-03-15';
SELECT DATEDIFF(minute, DepartsAt, ArrivesAt) AS Minutes,
       SWITCHOFFSET(DepartsAt, '+00:00') AS DepartsUtc,
       CAST(DepartsAt AS datetime2(0)) AS OffsetDropped
FROM dbo.Flight;

The tickets keep .002 and 23:59:59.999 as written, and the half-open range counts the two from March 14. The flight now lasts 570 minutes. SWITCHOFFSET shows its departure as 01:45 UTC on March 15. The last column is the trap on the way out: a cast to datetime2 drops the offset and keeps the local clock time, 18:45.

Comparing and converting datetime and datetime2

Back in the first database, the application looks up the ticket stored as 09:30:00.003, passing the time as a datetime2(3):

DECLARE @issued datetime2(3) = '2026-03-14 09:30:00.003';
SELECT COUNT(*) AS Found FROM dbo.Ticket
WHERE IssuedAt = @issued;
SELECT COUNT(*) AS Found FROM dbo.Ticket
WHERE IssuedAt = CAST(@issued AS datetime);
SELECT CAST(IssuedAt AS datetime2(7)) AS Stored FROM dbo.Ticket
WHERE TicketId = 2;

The first query finds nothing. When the two types meet, SQL Server converts the datetime to datetime2, the type with the higher data type precedence, the rule by which an nvarchar parameter converts a varchar column in nvarchar vs varchar. That ticket’s exact value is 1/300 of a second past 09:30, which the third query shows as .0033333. The parameter’s .003 is a different number. Since compatibility level 130, the level of SQL Server 2016, conversions to datetime2 keep those fractions, so values that used to compare equal no longer do. The datetime2 page advises an explicit cast wherever the two types are compared. The second query casts the parameter to the column’s datetime and finds the ticket. Better still, give the column and the parameter one type.

Converting the column itself keeps every value: in a run, ALTER COLUMN to datetime2(7) turned .003 into .0033333 and .007 into .0066667. On IssuedAt it fails first with Msg 5074, because DF_Ticket_IssuedAt depends on the column; drop the default, change the type, and add it back with SYSDATETIME(). The way back to datetime rounds to the nearest .000, .003 or .007, and a year before 1753 fails with Msg 242.

Old code sometimes adds days with +. In the first database that works: on a datetime, IssuedAt + 1 is the same time the next day. In the redesigned database, where IssuedAt is datetime2(3), the same IssuedAt + 1 fails:

SELECT IssuedAt + 1 AS NextDay FROM dbo.Ticket;
Msg 206, Level 16, State 2, Line 1
Operand type clash: datetime2 is incompatible with int

DATEADD(day, 1, IssuedAt) works on datetime and datetime2 alike, and says what it adds.

GETDATE or SYSDATETIME?

GETDATE() returns a datetime, rounded as above. SYSDATETIME() returns a datetime2(7), SYSUTCDATETIME() the same in UTC, and SYSDATETIMEOFFSET() a datetimeoffset(7) with the server’s offset:

SELECT GETDATE()          AS [GETDATE],
       SYSDATETIME()      AS [SYSDATETIME],
       SYSUTCDATETIME()   AS [SYSUTCDATETIME],
       SYSDATETIMEOFFSET() AS [SYSDATETIMEOFFSET];
DECLARE @now datetime2(7) = GETDATE();
SELECT @now AS GetDateInDatetime2;

In a run, GETDATE() gave 12:45:53.593 and SYSDATETIME() 12:45:53.5949380. Assigned to a datetime2(7), the GETDATE() value became 12:45:53.5933333: the 1/300-second ticks carried into the new type. A DEFAULT GETDATE() on a datetime2 column does the same to every row it fills, so give such a column SYSDATETIME() or SYSUTCDATETIME() as its default.

How ERDsmith helps

The airline’s bugs came from types picked once, in CREATE TABLE: a report that counted the wrong day, a lookup that missed a ticket, a flight seven hours too long. Each fix was a column’s type, and changing one in a live database meant Msg 5074 and a dropped default first. In ERDsmith the types are part of the drawing:

  • With Types on, the diagram shows every column’s type, so a datetime left among the datetime2 columns stands out.
  • The column panel offers Precision for datetime2, datetimeoffset and time, and a precision left unstated is 7, as in SQL Server.
  • A foreign key column takes its parent column’s type, length, precision and scale, and follows it when the parent’s type changes.
  • A child column whose precision differs from its parent’s is drawn without that line, never built, since SQL Server refuses that foreign key with Msg 1753.