SQL DATETIME2 is a date and time data type in SQL Server that was introduced in SQL Server 2008. It offers greater precision and a larger range of values than the DATETIME data type, making it a better choice for applications that require precise time measurement.


The syntax for declaring a column of type DATETIME2 in SQL Server is:

column_name DATETIME2(precision)

Here, “column_name” is the name of the column you want to create, and “precision” specifies the number of digits to use for fractional seconds. The precision can range from 0 to 7.


For example, to create a table with a DATETIME2 column called “timestamp” that has a precision of 3, you would use the following SQL code:

    timestamp DATETIME2(3)

You can also insert values into a DATETIME2 column using the ISO 8601 format, which is “YYYY-MM-DDThh:mm:ss[.nnnnnnn]”. Here’s an example:

INSERT INTO myTable (id, timestamp)
VALUES (1, '2023-05-01T15:30:00.1234567');

This inserts a record with an ID of 1 and a timestamp of May 1, 2023 at 3:30:00 PM and 123.4567 microseconds.

Overall, the DATETIME2 data type provides greater precision and flexibility for storing date and time values in SQL Server, making it a useful tool for a wide range of applications.