The MySQL ADDDATE function is a date and time function that allows you to add a specified time interval to a date or datetime value. This function is particularly useful when you need to perform calculations involving date and time in your MySQL queries.
Syntax
The syntax for the ADDDATE function is as follows:
ADDDATE(date, INTERVAL expr unit)
Here, date is the initial date or datetime value, expr is the numeric expression representing the amount you want to add, and unit specifies the time unit (e.g., DAY, MONTH, YEAR).
Examples
Let’s explore a few examples to illustrate how the ADDDATE function works:
Adding days to a date:
SELECT ADDDATE('2022-01-01', INTERVAL 7 DAY) AS new_date;
This query adds 7 days to the date ‘2022-01-01’, resulting in the new date ‘2022-01-08’.
Adding months to a datetime value:
SELECT ADDDATE('2022-01-01 12:00:00', INTERVAL 3 MONTH) AS new_datetime;
Here, 3 months are added to the datetime value ‘2022-01-01 12:00:00’, yielding the new datetime ‘2022-04-01 12:00:00’.
Adding years to a date:
SELECT ADDDATE('2022-01-01', INTERVAL 2 YEAR) AS new_date;
This query adds 2 years to the date ‘2022-01-01’, resulting in the new date ‘2024-01-01’.
It’s important to note that the ADDDATE function doesn’t modify the original date or datetime value; instead, it returns a new value based on the addition. The function is versatile, allowing you to perform various date and time calculations in your MySQL queries. Additionally, you can use negative values for expr to subtract time intervals from a date or datetime value.
Keep in mind that the ADDDATE function is just one of several date and time functions available in MySQL, offering flexibility and efficiency when working with temporal data in your database queries.