Relational DatabaseSQL

SQL – Working with Date and Time

SQL Databases include various data types that help us store date and time in the database. Some of these data types are DATE, TIME, DATETIME, TIMESTAMP, and YEAR.
In addition to these, some databases may include additional data types for handling these types of data.

Commonly used Data Types for Date and Time

DATE - Used to store a date in the format (YYYY-MM-DD).
TIME - Used to store time in the format (HH-MM-SS).
DATETIME - Used to store both the date and time in the format (YYYY-MM-DD HH-MM-SS).
TIMESTAMP - Used to store the date and time in the format (YYYY-MM-DD HH-MM-SS).
YEAR - Used to store the year in the format (YYYY).

Examples
Insert a record with the date
INSERT INTO databaseTable (dateColumn) Values ('2023-01-01');
Insert a record with the time
INSERT INTO databaseTable (timeColumn) Values ('15-30-33');
Insert a record with the date and time
INSERT INTO databaseTable (dateTimeColumn) Values ('2023-01-01 15-30-33');
Add a new column to an existing database table to store dates.
ALTER TABLE databaseTable ADD COLUMN (dateTimeColumn date);

Some date and time SQL functions

CURRENT_DATE - A function used to return the current date.
CURRENT_TIME - A function used to return the current time.

Examples
Insert a record with the current date
INSERT INTO databaseTable (dateColumn) Values (CURRENT_DATE);
Insert a record with the current date and time
INSERT INTO databaseTable (dateTimeColumn) Values (CURRENT_TIME);

Note: Depending on the type of database, you may have a wide range of additional functions and data types for manipulating date and time data.

What's your reaction?

Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0

You may also like

Leave a reply

Your email address will not be published. Required fields are marked *

Time limit exceeded. Please complete the captcha once again.