0% found this document useful (0 votes)
0 views9 pages

03 Perform Date and Time Functions in MySQL

This document outlines a lesson on performing date and time functions in MySQL, detailing the necessary steps to manipulate date and time data types. It includes instructions for logging into MySQL, creating a database and table, and utilizing various functions such as NOW, CURDATE, DATE, TIME, DATEDIFF, ADD, SUBTRACT, and EXTRACT. Sample SQL code is provided for each function to demonstrate their usage.

Uploaded by

Prince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views9 pages

03 Perform Date and Time Functions in MySQL

This document outlines a lesson on performing date and time functions in MySQL, detailing the necessary steps to manipulate date and time data types. It includes instructions for logging into MySQL, creating a database and table, and utilizing various functions such as NOW, CURDATE, DATE, TIME, DATEDIFF, ADD, SUBTRACT, and EXTRACT. Sample SQL code is provided for each function to demonstrate their usage.

Uploaded by

Prince
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy