SQL Server Date and Time Data Types

Microsoft Learn | Documentation

On this page:

Introduction

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.

datetime2

The 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
);

Precision

You can specify the precision for datetime2 when declaring the column:

smalldatetime

smalldatetime 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.

datetime

The 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');

datetimeoffset

datetimeoffset stores date and time data along with time zone information. This is particularly useful when working with data from multiple time zones.

INSERT INTO Events (EventID, EventName, EventDateTime)
VALUES (2, 'Global Meeting', '2023-10-27 15:00:00.000 +01:00');

date

The 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
);

time

The 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
);

Overview Table

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

Comparison and Considerations

When choosing a data type, consider the following:

Related API References

Important Note on Precision

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.

Recommendations