Microsoft Learn | Documentation
SQL Server provides a rich set of data types for storing and manipulating date and time information. Choosing the right data type is crucial for data integrity, performance, and storage efficiency. This documentation covers the primary date and time data types available in SQL Server.
datetime2The datetime2 data type is the recommended data type for storing date and time values in newer versions of SQL Server. It offers a wider range of dates and higher precision than the older datetime type.
CREATE TABLE Events (
EventID INT PRIMARY KEY,
EventName VARCHAR(100),
EventDateTime DATETIME2(3) -- Stores date and time with millisecond precision
);
You can specify the precision for datetime2 when declaring the column:
DATETIME2(0): SecondsDATETIME2(1): Tenths of a secondDATETIME2(2): Hundredths of a secondDATETIME2(3): MillisecondsDATETIME2(4): Ten-thousandths of a secondDATETIME2(5): Hundred-thousandths of a secondDATETIME2(6): MicrosecondsDATETIME2(7): Nanosecondssmalldatetimesmalldatetime is an older data type that stores date and time values with less precision and a smaller range compared to datetime and datetime2.
Note: While it uses less storage, its limited range and precision make it less suitable for most modern applications.
datetimeThe datetime data type is a widely used older data type for storing date and time values. It has a broader range than smalldatetime but less precision than datetime2.
INSERT INTO Events (EventID, EventName, EventDateTime)
VALUES (1, 'System Start', '2023-10-27 10:30:00.123');
datetimeoffsetdatetimeoffset stores date and time data along with time zone information. This is particularly useful when working with data from multiple time zones.
datetime2 (January 1, 0001 to December 31, 9999)datetime2 (up to 100 nanoseconds).INSERT INTO Events (EventID, EventName, EventDateTime)
VALUES (2, 'Global Meeting', '2023-10-27 15:00:00.000 +01:00');
dateThe date data type stores only the date portion of a date and time value.
CREATE TABLE Birthdays (
PersonID INT PRIMARY KEY,
Name VARCHAR(100),
BirthDate DATE
);
timeThe time data type stores only the time portion of a date and time value.
CREATE TABLE DailySchedule (
ScheduleID INT PRIMARY KEY,
Activity VARCHAR(100),
StartTime TIME(0) -- Stores time without fractional seconds
);
Here's a summary of the key date and time data types:
| Data Type | Range | Precision | Storage | Time Zone |
|---|---|---|---|---|
date |
0001-01-01 to 9999-12-31 | Day | 3 bytes | No |
time |
00:00:00.0000000 to 23:59:59.9999999 | 100 nanoseconds (default 7) | 3-5 bytes | No |
smalldatetime |
1900-01-01 to 2079-06-06 | Minute | 4 bytes | No |
datetime |
1753-01-01 to 9999-12-31 | 0.003 seconds | 8 bytes | No |
datetime2 |
0001-01-01 to 9999-12-31 | 100 nanoseconds (default 7) | 6-8 bytes | No |
datetimeoffset |
0001-01-01 to 9999-12-31 | 100 nanoseconds (default 7) | 10 bytes | Yes |
When choosing a data type, consider the following:
datetime2 or time are suitable.smalldatetime has a limited range. If your data extends beyond 1900 or 2079, use datetime or datetime2.datetimeoffset is the best choice.date is the most efficient. For times without dates, use time.datetime, you might continue using it, but for new development, datetime2 is generally preferred.The precision of datetime2 and time can be explicitly defined. For instance, DATETIME2(3) stores milliseconds, while DATETIME2(7) stores nanoseconds. The default precision is 7 digits.
datetime2 unless specific reasons dictate otherwise.date for storing only dates.time for storing only times.datetimeoffset when time zone awareness is critical.smalldatetime for new applications due to its limitations.