Examples related to DateTime
Let’s understand the DateTime data type with a simple example given below:
Query
-- 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.
Output
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.
Difference Between DateTime and SmallDateTime in SQL Server
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:
- DateTime
- SmallDateTime
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.