In MySQL, you can achieve the functionality of adding months to a date using the DATE_ADD() function. Here's how you can use it:
DATE_ADD(date, INTERVAL n MONTH)
- date: This is the date to which you want to add months.
- n: This is the number of months to add. It can be positive (to add months) or negative (to subtract months).
Let's see an example to understand how DATE_ADD() works in MySQL:
Suppose we have a date '2022-01-15', and we want to add 3 months to it.
SELECT DATE_ADD('2022-01-15', INTERVAL 3 MONTH) AS new_date;
This query will return the result:
new_date
2022-04-15
In this example, the DATE_ADD() function adds 3 months to the date '2022-01-15', resulting in '2022-04-15'.
Similarly, you can use negative values to subtract months from a date. For example, to subtract 6 months from a date:
SELECT DATE_ADD('2022-01-15', INTERVAL -6 MONTH) AS new_date;
This would result in '2021-07-15'.
No comments:
Post a Comment