MySQL ADDDATE() Function
The ADDDATE()
function in MySQL allows you to add or subtract a time interval to or from a date, making it easy to calculate future or past dates. It's a very useful tool for date manipulation.
ADDDATE(): Definition and Usage
ADDDATE()
lets you adjust a date by adding or subtracting various time units (days, months, years, hours, minutes, seconds, etc.). It's much more flexible than simply adding or subtracting numbers to a date because you can specify the *type* of interval you're adding.
Syntax
Syntax
ADDDATE(date, INTERVAL value unit)
--or--
ADDDATE(date, days) --For adding/subtracting only days.
Parameter Values
Parameter | Description |
---|---|
date |
The starting date. This is required. |
value |
The number of intervals to add (positive) or subtract (negative). This is required. |
unit |
The unit of time. Options: MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , YEAR , SECOND_MICROSECOND , MINUTE_MICROSECOND , MINUTE_SECOND , HOUR_MICROSECOND , HOUR_SECOND , HOUR_MINUTE , DAY_MICROSECOND , DAY_SECOND , DAY_MINUTE , DAY_HOUR , YEAR_MONTH . This is required. |
days |
The number of days to add (positive) or subtract (negative). Used in the simplified syntax. This is required when using simplified syntax. |
Examples
Adding Days to a Date
Adding 10 days to June 15th, 2017.
Syntax
SELECT ADDDATE("2017-06-15", INTERVAL 10 DAY);
Output
2017-06-25
Adding Minutes to a Datetime
Adding 15 minutes to a datetime value.
Syntax
SELECT ADDDATE("2017-06-15 09:34:21", INTERVAL 15 MINUTE);
Output
2017-06-15 09:49:21
Subtracting Hours from a Datetime
Subtracting 3 hours from a datetime value.
Syntax
SELECT ADDDATE("2017-06-15 09:34:21", INTERVAL -3 HOUR);
Output
2017-06-15 06:34:21
Subtracting Months from a Date
Subtracting 2 months from a date.
Syntax
SELECT ADDDATE("2017-06-15", INTERVAL -2 MONTH);
Output
2017-04-15