03 Perform Date and Time Functions in MySQL
03 Perform Date and Time Functions in MySQL
Objective: To demonstrate how to use the date and time functions to manipulate
date and time data types in MySQL
Prerequisites: None
Steps to be followed:
1. Log in to the MySQL Database
2. Create a Database and Table
3. Use the NOW function
4. Use the CURDATE function
5. Use the DATE function
6. Use the TIME function
7. Use the DATEDIFF function
8. Use the ADD function
9. Use the SUBTRACT function
10. Use the EXTRACT function
3.1 Retrieve the current date and time using the NOW() function
SQL Code:
SELECT id, event_name, NOW() AS current_datetime FROM datetime_info;
6.1 Extract the time part from the event_datetime column using the TIME()
function
SQL Code:
SELECT id, event_name, TIME(event_datetime) AS time_only FROM
datetime_info;
7.1 Calculate the difference in days between the current date and the
event_datetime column using the DATEDIFF() function
SQL Code:
SELECT id, event_name, DATEDIFF(NOW(), event_datetime) AS
days_difference FROM datetime_info;
Step 8: Use the ADD function
9.1 Subtract 1 day from the event_datetime column using the DATE_SUB()
function
SQL Code:
SELECT id, event_name, DATE_SUB(event_datetime, INTERVAL 1 DAY) AS
subtracted_days FROM datetime_info;
10.1 Extract the minute part from the event_datetime column using the
EXTRACT() function
SQL Code:
SELECT id, event_name, EXTRACT(MINUTE FROM event_datetime) AS
extracted_minute FROM datetime_info;
Date and time functions are executed on a specific dataset using these
procedures.