![]() |
VOOZH | about |
SQL Server datatypes are used to store date and date and time values in the database, there are various types of date data types available in the SQL. Whenever we manage data in the SQL server database, itβs often very important to choose the right to store the date and time.
The following two data types are used for storing the date and time in SQL Server:
Both the above choices have their impact on how much storage is required to store the data, how fast the queries can be, and the accuracy of the data as well. so in this article, weβll understand the key differences between both of the data types and how they impact the way we store data in the database.
Feature | smallDatetime | DateTime |
|---|---|---|
Accuracy | One minute | Rounded to increments of .000, .003, or .007 seconds. |
Fractional second precision | No | Yes |
Time Zone Offset | None | None |
Storage Size | 4 Bytes, Fixed. | 8 Bytes, Fixed. |
Character length | 19 positions maximum | 19 positions minimum |
Time Range | 00:00:00 through 23:59:59 | 00:00:00 through 23:59:59.997 |
Date Range | 1900-01-01 through 2079-06-06 | 1753-01-01 through 9999-12-31 |
Note β The Oracle documentation sometimes uses DATETIME when it is talking about the DATE datatype and the TIMESTAMP data type together, and since we are focused on DATETIME and SMALLDATETIME (which are part of MS SQL Server) we will be using the MS SQL Server for running the queries.
Letβs understand the DateTime data type with a simple example given below:
-- Creating a table with DateTime data type
CREATE TABLE DateTimeExample (
EventName NVARCHAR(50),
EventDate DateTime
)
-- Inserting data with DateTime
INSERT INTO DateTimeExample (EventName, EventDate)
VALUES ('Meeting', '2023-10-30 14:30:00')
-- Querying data
SELECT * FROM DateTimeExample
Program Explanation: Here, in the above query first we create a table named βDateTimeExampleβ with two columns (EventName, EventDate) where the βEventDateβ holds the DateTime data type. Now when we insert data into this table.
Here, we can see that the EventDate shows the time with milliseconds if the user defines it, which is specifically the purpose of using the DateTime data type, so if we want to show time with further accuracy then we can use the DateTime data type.
Letβs understand the SmallDateTime data type with a simple example given below:
-- Creating a table with SmallDateTime data type
CREATE TABLE SmallDateTimeExample (
EventName NVARCHAR(50),
EventDate SmallDateTime
)
-- Inserting data with SmallDateTime
INSERT INTO SmallDateTimeExample (EventName, EventDate)
VALUES ('Appointment', '2023-10-30 14:30:00')
-- Querying data
SELECT * FROM SmallDateTimeExample
Again, we have created a table named βSmallDateTimeExampleβ where we have two columns, EventName and EventDate and we have given the βEvenDateβ column the βSmallDateTimeβ data type and then we have inserted some values into the table.
Here we get to understand the key difference between the SQL Server DateTime and SmallDateTime data types in MS Server, the βSmallDateTimeβ does not provide as much accuracy as compared to the βDateTimeβ data type hence itβs used for storing data and time data values where fractional parts of the milliseconds are optional.
In conclusion, both of the date data types are useful and have their own functionality, if the database administrator needs to have more data accuracy then they can opt for the 'DateTime' datatype otherwise the 'SmallDateTime' can be used.