In MySQL, you can use the ADDDATE()
function to add a specified amount of time to a date. For example, you could use it to add 10 days to a given date. You can specify whether to add days, weeks, months, quarters, years, etc. You can also add a time value, such as seconds, microseconds, etc.
The ADDDATE()
function is a synonym for the DATE_ADD()
function (they both do the same thing) when using the first syntax listed below.
This article contains examples to demonstrate usage of the ADDDATE()
function.
Syntax
You can use this function in the following two ways:
ADDDATE(date,INTERVAL expr unit)
Or
ADDDATE(expr,days)
Example 1 – The First Syntax
Here’s an example of using the first form of the syntax.
SELECT ADDDATE('2018-12-01', INTERVAL 2 DAY) AS Result;
Result:
+------------+ | Result | +------------+ | 2018-12-03 | +------------+
This example adds 2 days to the date supplied by the first argument.
Example 2 – The Second Syntax
This example could be rewritten to the following:
SELECT ADDDATE('2018-12-01', 2) AS Result;
Result:
+------------+ | Result | +------------+ | 2018-12-03 | +------------+
This uses the second form of the syntax. The second argument is an integer that represents how many days should be added to the date supplied by the first argument. So obviously, this form is only suitable if you want to specify the amount in days.
Note that ADDDATE()
is a synonym for DATE_ADD()
only when the first syntax is being used. The second syntax is only available in ADDDATE()
.
Example 3 – Other Date Units
One benefit of the first form of the syntax is that you can specify whether to add days, weeks, months, years, etc. Here are some examples.
SELECT '2018-12-01' AS 'Start Date', ADDDATE('2018-12-01', INTERVAL 2 WEEK) AS '+2 Weeks', ADDDATE('2018-12-01', INTERVAL 2 MONTH) AS '+2 Months', ADDDATE('2018-12-01', INTERVAL 2 QUARTER) AS '+2 Quarters', ADDDATE('2018-12-01', INTERVAL 2 YEAR) AS '+2 Years';
Result:
+------------+------------+------------+-------------+------------+ | Start Date | +2 Weeks | +2 Months | +2 Quarters | +2 Years | +------------+------------+------------+-------------+------------+ | 2018-12-01 | 2018-12-15 | 2019-02-01 | 2019-06-01 | 2020-12-01 | +------------+------------+------------+-------------+------------+
Example 4 – Time Units
You can also use ADDDATE()
to add time units to a date/time value. Here’s an example.
SELECT ADDDATE('2018-12-01 01:00:00', INTERVAL 2 HOUR) AS Result;
Result:
+---------------------+ | Result | +---------------------+ | 2018-12-01 03:00:00 | +---------------------+
Expected Values
The following table shows the valid unit values and their expected format.
unit Value |
Expected expr Format |
---|---|
MICROSECOND | MICROSECONDS |
SECOND | SECONDS |
MINUTE | MINUTES |
HOUR | HOURS |
DAY | DAYS |
WEEK | WEEKS |
MONTH | MONTHS |
QUARTER | QUARTERS |
YEAR | YEARS |
SECOND_MICROSECOND | ‘SECONDS.MICROSECONDS’ |
MINUTE_MICROSECOND | ‘MINUTES:SECONDS.MICROSECONDS’ |
MINUTE_SECOND | ‘MINUTES:SECONDS’ |
HOUR_MICROSECOND | ‘HOURS:MINUTES:SECONDS.MICROSECONDS’ |
HOUR_SECOND | ‘HOURS:MINUTES:SECONDS’ |
HOUR_MINUTE | ‘HOURS:MINUTES’ |
DAY_MICROSECOND | ‘DAYS HOURS:MINUTES:SECONDS.MICROSECONDS’ |
DAY_SECOND | ‘DAYS HOURS:MINUTES:SECONDS’ |
DAY_MINUTE | ‘DAYS HOURS:MINUTES’ |
DAY_HOUR | ‘DAYS HOURS’ |
YEAR_MONTH | ‘YEARS-MONTHS’ |