Lesson 04 Demo 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
Tools required: MySQL Workbench
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
Step 1: Log in to the MySQL Database
1.1 Connect to your MySQL server using a MySQL client or command-line
interface
Step 2: Create a Database and Table
2.1 Create the datetime_database by executing the following SQL
statement:
SQL Code:
CREATE DATABASE IF NOT EXISTS datetime_database;
USE datetime_database;
2.2 Create the datetime_info table within the datetime_database with
columns for id INT PRIMARY KEY, event_name VARCHAR(100), event_datetime
DATETIME
SQL Code:
CREATE TABLE IF NOT EXISTS datetime_info (
id INT PRIMARY KEY,
event_name VARCHAR(100),
event_datetime DATETIME
);
2.3 Insert sample data into the datetime_info table:
SQL Code:
INSERT INTO datetime_info (id, event_name, event_datetime) VALUES
(1, 'Event 1', '2022-01-15 14:30:00'),
(2, 'Event 2', '2022-02-20 18:45:00'),
(3, 'Event 3', '2022-03-25 10:00:00');
Step 3: Use the NOW 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;
Step 4: Use the CURDATE function
4.1 Retrieve the current date using the CURDATE() function
SQL Code:
SELECT id, event_name, CURDATE() AS current_date FROM datetime_info;
Step 5: Use the DATE function
5.1 Write a SELECT query using the DATE() function
SQL Code:
SELECT id, event_name, DATE(event_datetime) AS date_only FROM
datetime_info;
Step 6: Use the TIME function
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;
Step 7: Use the DATEDIFF function
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
8.1 Write a SELECT query using the DATE_ADD() function
SQL Code:
SELECT id, event_name, DATE_ADD(event_datetime, INTERVAL 3 HOUR)
AS added_hours FROM datetime_info;
Step 9: Use the SUBTRACT 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;
Step 10: Use the EXTRACT function
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.