JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Adding Date and Time in MySQL using Date_ADD() Function

Author:JIYIK Last Updated:2025/04/23 Views:

This article shows you how to add dates and times in MySQL using DATE_ADD()the _date_time_s() function. We will learn how to add or subtract days, years, months, and times individually, as well as how to combine the two (for example, days and hours).


DATE_ADD() Method in MySQL

We can DATE_ADD()add a time interval to a value of type DATETIME or DATE using the method. It accepts two parameters.

The first parameter is start_date, which is a start date. It can be of DATE/DATETIME type.

The second argument is the INTERVAL expression unit, which is the interval value to which the start_date value should be added. You can find a list of units that we can use this method with.

If the first argument is a DATETIME or interval value containing a time element (hours, minutes, or seconds), it returns a DATETIME value; otherwise, it returns a string.


Create a table in MySQL and insert data

To learn DATE_ADD()the method, let's create a table called trips with columns id, country_name, and startDate where startDate is of type DATETIME. You can also create a trips table and fill it with some random data given below to follow along.

CREATE TABLE trips(
  id INT NOT NULL AUTO_INCREMENT,
  country_name VARCHAR(45) NOT NULL,
  startDate DATETIME NOT NULL,
  PRIMARY KEY (id));

INSERT INTO trips (country_name, startDate) VALUES
('Pakistan', '2019-01-27 01:23:34'),
('USA', '2019-02-22 12:34:05'),
('Turkey', '2020-05-14 08:03:02'),
('India', '2020-01-21 11:20:04');

SELECT * FROM trips;

Output:

| id   | country_name | startDate           |
| ---- | ------------ | ------------------- |
| 1    | Pakistan     | 2019-01-27 01:23:34 |
| 2    | USA          | 2019-02-22 12:34:05 |
| 3    | Turkey       | 2020-05-14 08:03:02 |
| 4    | India        | 2020-01-21 11:20:04 |

Adding Days in MySQL Using DATE_ADD() Function

Add 2 days to the startDate of the trips table with id 3.

Sample code:

UPDATE trips
SET startDate = DATE_ADD(startDate , INTERVAL 2 DAY)
WHERE id = 3;

Output:

| id   | country_name | startDate           |
| ---- | ------------ | ------------------- |
| 1    | Pakistan     | 2019-01-27 01:23:34 |
| 2    | USA          | 2019-02-22 12:34:05 |
| 3    | Turkey       | 2020-05-16 08:03:02 |
| 4    | India        | 2020-01-21 11:20:04 |

We can also subtract days as follows.

Sample code:

UPDATE trips
SET startDate = DATE_ADD(startDate , INTERVAL -2 DAY)
WHERE id = 1;

Output:

| id   | country_name | startDate           |
| ---- | ------------ | ------------------- |
| 1    | Pakistan     | 2019-01-25 01:23:34 |
| 2    | USA          | 2019-02-22 12:34:05 |
| 3    | Turkey       | 2020-05-16 08:03:02 |
| 4    | India        | 2020-01-21 11:20:04 |

Add Month and Year in MySQL using DATE_ADD() Function

Like days, we can also add years and months. To subtract a number of years or months, write an expression with a minus sign (-ve).

Sample code:

#add month
UPDATE trips
SET startDate = DATE_ADD(startDate , INTERVAL 1 MONTH)
WHERE id = 1;

#add year
UPDATE trips
SET startDate = DATE_ADD(startDate , INTERVAL 1 YEAR)
WHERE id = 1;

Output:

| id   | country_name | startDate           |
| ---- | ------------ | ------------------- |
| 1    | Pakistan     | 2020-02-25 01:23:34 |
| 2    | USA          | 2019-02-22 12:34:05 |
| 3    | Turkey       | 2020-05-16 08:03:02 |
| 4    | India        | 2020-01-21 11:20:04 |

Add hours, minutes and seconds in MySQL using DATE_ADD() function

Add 1 hour, minute, and second to startDate in the trips table with id 4.

Sample code:

#add hours
UPDATE trips
SET startDate = DATE_ADD(startDate , INTERVAL 1 HOUR)
WHERE id = 4;

#add minutes
UPDATE trips
SET startDate = DATE_ADD(startDate , INTERVAL 1 MINUTE)
WHERE id = 4;

#add seconds
UPDATE trips
SET startDate = DATE_ADD(startDate , INTERVAL 1 SECOND)
WHERE id = 4;

Output:

| id   | country_name | startDate           |
| ---- | ------------ | ------------------- |
| 1    | Pakistan     | 2020-02-25 01:23:34 |
| 2    | USA          | 2019-02-22 12:34:05 |
| 3    | Turkey       | 2020-05-16 08:03:02 |
| 4    | India        | 2020-01-21 12:21:05 |

Add or subtract dates and times in MySQL using the DATE_ADD() function

Add 2 hours and 2 minutes to the startDate of the trips table with id 3.

#add hours and minutes
UPDATE trips
SET startDate = DATE_ADD(startDate , INTERVAL '2:2' HOUR_MINUTE)
WHERE id = 3;

Output:

| id   | country_name | startDate           |
| ---- | ------------ | ------------------- |
| 1    | Pakistan     | 2020-02-25 01:23:34 |
| 2    | USA          | 2019-02-22 12:34:05 |
| 3    | Turkey       | 2020-05-16 10:05:02 |
| 4    | India        | 2020-01-21 12:21:05 |

Likewise, we can use DAY_HOUR, YEAR_MONTH, etc.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Display tables and database structure in MySQL

Publish Date:2025/04/23 Views:97 Category:MySQL

Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi

Select first row from MySQL table

Publish Date:2025/04/23 Views:112 Category:MySQL

Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic

Insert timestamp into MySQL table

Publish Date:2025/04/23 Views:77 Category:MySQL

Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T

The difference between two tables in MySQL

Publish Date:2025/04/23 Views:102 Category:MySQL

In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta

MySQL sorts data alphabetically

Publish Date:2025/04/23 Views:129 Category:MySQL

In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t

Display the current database in MySQL

Publish Date:2025/04/23 Views:199 Category:MySQL

This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query

Check if a database exists in MySQL

Publish Date:2025/04/23 Views:179 Category:MySQL

In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve

Get the sum of multiple columns in MySQL

Publish Date:2025/04/23 Views:125 Category:MySQL

In this article, we will learn how to sum multiple columns in MySQL. Sum multiple columns in MySQL You can use aggregate functions SUM() to calculate the total value in a collection. SUM() The function calculation does not consider NULL val

MySQL ForEach Loop

Publish Date:2025/04/23 Views:164 Category:MySQL

This article describes how to simulate a foreach loop in MySQL using INSERT, SELECT, WHERE, and JOIN in one statement. MySQL foreach loop To understand foreach loop simulation, let us create three tables with the following names and attribu

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial