In SQL, Dates and Times are handled using various data types and it is used for querying data or formatting date values. SQL provides various functions and methods to manage and manipulate dates.
Dates and Time Functionality essential is used in applications such as Chat Applications, E-Commerce Applications, Banking, Fintech and more.
Stores the date only, without any time information.
CREATE TABLE tableName ( dateColumn DATE );
Stores the time only, without any date information.
CREATE TABLE tableName ( timeColumn TIME );
Stores both date and time information.
CREATE TABLE tableName ( datetimeColumn DATETIME );
Similar to DATETIME but often used with automatic updating to the current timestamp when a record is inserted or updated.
CREATE TABLE tableName ( timestampColumn TIMESTAMP );
when dealing with dates, it's important to use a format that is compatible with the chosen date-related data type.
using the below INSERT INTO Statement, we can Insert a Date into the record of the SQL Table.
INSERT INTO products (product_added_date) VALUES ('2022-03-03');
using the below INSERT INTO Statement, we can Insert a time into the record of the SQL Table.
INSERT INTO products (product_added_time) VALUES ('02:20:00');
using the below INSERT INTO Statement, we can Insert a DateTime into the record of the SQL Table.
INSERT INTO products (product_added_date_time) VALUES ('2022-03-03 03:30:00');
using the below INSERT INTO Statement, we can Insert a timestamp which is often automatically updated.
INSERT INTO products (product_added_timestamp) VALUES (CURRENT_TIMESTAMP);
The format 'YYYY-MM-DD' is commonly used for dates, 'HH:MM:SS' for times, and 'YYYY-MM-DD HH:MM:SS' for datetimes.
SELECT MONTH(date_column) AS month FROM tableName GROUP BY MONTH(date_column);
SELECT MONTH(product_added_date) AS month FROM products GROUP BY MONTH(product_added_date);
SELECT * FROM table_name ORDER BY date_column DESC;
SELECT * FROM products ORDER BY product_added_date DESC;