MySQL DATE_ADD() function
In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples.
Introduction of MySQL DATE_ADD() function
In MySQL, the DATE_ADD() function is used to add a mentioned time or date interval to a specific date and then return the date.
Syntax of the MySQL DATE_ADD() function
The syntax of the MySQL DATE_ADD() function is given as follows:
DATE_ADD(dateM, INTERVAL value addedunit)
Parameters or arguments used in MySQL DATE_ADD() function:
There are two parameters accepted by the DATE_ADD() function in MySQL, which are given as follows:
DateM: It is the mentioned date to be Modified.
Value addedunit: Here the value parameter is the date or time interval to be added. This value can be both positive and negative. And here, the add-unit is the type of interval to add, such as SECOND, MINUTE, HOUR, DAY, YEAR, MONTH, etc.
Returns:
It will return the new date after the addition of the mentioned time or date.
Application used for DATE_ADD() function:
The DATE_ADD() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL DATE_ADD() function:
Now we will look into some MySQL DATE_ADD() function examples and will explore how we can use the DATE_ADD function in MySQL.
Example 1:
In this example, we will get a new date of "2020-11-22" after the addition of 3 years to the specified date "2017-11-22" in MySQL.
SELECT DATE_ADD("2017-11-22", INTERVAL 3 YEAR);
Output:
2020-11-22
1 row in set (0.00 sec)
Example 2:
In this example, we will get a new date of "2020-11-22" after the addition of 2 months to the specified date "2020-9-22" in MySQL.
SELECT DATE_ADD("2020-9-22", INTERVAL 2 MONTH);
Output:
2020-11-22
1 row in set (0.00 sec)
Example 3:
In this example, we will get a new date of "2020-11-22" after the addition of 10 days to the specified date "2020-11-12" in MySQL.
SELECT DATE_ADD("2020-11-12", INTERVAL 10 DAY);
Output:
2020-11-22
1 row in set (0.00 sec)
Example 4:
In this example, we will get a new "2020-11-22 09:12:10" date after adding 3 hours to the specified date "2020-11-22 06:12:10".
SELECT DATE_ADD("2020-11-22 06:12:10", INTERVAL 3 HOUR);
Output:
2020-11-22 09:12:10
1 row in set (0.00 sec)
Example 5:
In this example, we will get a new date of "2020-11-22 09:09:10" after the addition of 3 minutes to the specified date "2020-11-22 09:06:10".
SELECT DATE_ADD("2020-11-22 09:06:10", INTERVAL 3 MINUTE);
Output:
2020-11-22 09:09:10
1 row in set (0.00 sec)
Example 6:
In this example, we will get a new date of "2020-11-22 09:09:10" after the addition of 5 seconds to the specified date "2020-11-22 09:09:5" in MySQL.
SELECT DATE_ADD("2020-11-22 09:09:5", INTERVAL 5 SECOND);
Output:
2020-11-22 09:09:10
1 row in set (0.00 sec)
Application of MySQL DATE_ADD() function:
This function is used to add a mentioned time or date interval to a mentioned date and then return the date.
Summary:
In the above context, we have learned how we can use the DATE_ADD() function in MySQL used to add a mentioned time or date interval to a mentioned date and then return the date.