SQL DATETIME Vs DATETIME 2 Datatype

The distinction between Datetime and Datetime2 functions will be discussed in this post. Instead of Datetime , Microsoft recommends using Datetime2 . As a result, we'll compare and contrast them in this section.


SQL DATETIME Datatype

The DATETIME data type specifies a fractional second date and time.

The DateTime data type is the most popular for storing date and time information together.

Dates from January 1, 1753, to December 31, 9999 are supported by DATETIME.

1900-01-01 00:00:00 is the default value. The time is shown in 24-hour format.

Example: A table with a DATETIME column.

Take note of how SQL Server takes a variety of date and time formats as input.

These formats are transformed to 'YYYY-MM-DD HH:MM:SS', which is the internal format.

CREATE TABLE Customer  
( 
  CustomerName VARCHAR(100) NOT NULL,
  Product VARCHAR(100) NOT NULL,
  OrderDateTime DATETIME
);


INSERT INTO Customer VALUES ('Lilly', 'Kitkat', '2020-09-25 9:55:12');
INSERT INTO Customer VALUES ('Vishal', 'Pasta', '2019/08/03 9AM');
INSERT INTO Customer VALUES ('Mani', 'Hide & seek Cookies', '2022-05-14 12:10');

SELECT * FROM Customer;

This datatype doesn't allow the year of below 1900.

Output: The output of above example is

CustomerName ProductName OrderDatetime
Lilly Kitkat 2020-09-25 9:55:12.0000000
Vishal pasta 2021-05-14 9:00:00.0000000
Mani Hide & seek Cookies 2022-05-14 12:10:00.0000000

SQL DATETIME 2 Datatype

The DATETIME2 type is an extension of the original Datetime type.

The DATETIME2 data type specifies a fractional second date and time.

The datetime2 variable in this case has a scale of 7, which signifies 7 decimal places.

Dates from 0001-01-01 to 9999-12-31 are supported by DATETIME2.

1900-01-01 00:00:00 is the default value. A 24-hour clock is used to keep track of time.

Example: The example creates a table with a DATETIME2 column.

CREATE TABLE Customer
( 
 
  CustomerName VARCHAR(100) NOT NULL,
  ProductName VARCHAR(100) NOT NULL,
  OrderDateTime DATETIME2
);



INSERT INTO Customer VALUES ('Ravi', 'Quaker Oats', '2021/12/10 8AM');
INSERT INTO Customer VALUES ('Jeni', 'Good day Cookies', '2022-05-14 13:12');
INSERT INTO Customer VALUES ('Karthi', 'Frutie', '2022-9-2');


SELECT * FROM Customer;

The datetime2 datatype doesn't allow daterange above the year of 9999

Output: The output of above example is

CustomerName ProductName OrderDatetime
Ravi Quaker Oats 2021-12-10 08:00:00.0000000
Jeni Good day Cookies 2021-05-14 13:12:00.0000000
Karthi Fruite 2021-09-02 00:00:00.0000000

SQL Difference Between DATETIME And DATETIME2 Datatype

In below table we discuss the distinguish datetime & datetime2 datatype:-

DATETIME DATETIME2
#.SQL Compliant (ANSI & ISO 8601) - No SQL Compliant (ANSI & ISO 8601) - yes
Date Range 1753-01-01 thru 9999-12-31 Larger date range 0001-01-01 thru 9999-12-31
Fractional precision is slower. Greater fractional precision as a default
It lacks the option of specifying accuracy by the user. It has a precision setting that may be changed by the user.
Low precision, even with the same amount of decimal places as datetime. Greater precision, even with the same amount of decimal places as datetime (i.e. 3)
Large storage space (8 bytes) when utilising the same number of decimal places as datetime, but with greater accuracy* Rounded to.000,.003, or.007 second increments Smaller storage capacity (6 to 8 bytes, depending on precision)* * Plus 1 byte to store precision when using the same number of decimal places as datetime, but with greater precision*a hundred nanoseconds
Time Range - 00:00:00 thru 23:59:59.997 Time Range - 00:00:00 thru 23:59:59.9999999
Character Length - 19 minimum 23 maximum Character Length - 19 minimum 27 maximum
Time Zone Offset - None Time Zone Offset - None
Time zone offset aware and preservation - No Time zone offset aware and preservation - Notd>
Daylight saving aware - No Daylight saving aware - No
When stored in a database, datetime uses 8 bytes . When stored in a database, datetime2(3) uses 7 bytes