Home >Database >Mysql Tutorial >How to Add One Year to a Date Value in MySQL?

How to Add One Year to a Date Value in MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-31 08:50:29963browse

How to Add One Year to a Date Value in MySQL?

Editing Date Values in MySQL: Adding One Year

In MySQL, you can directly increment numerical values using the SET number=number 1 syntax. However, working with dates requires a slightly different approach.

Question:

How do I add one year to a date value in a MySQL table?

Answer:

To add one year to a date field, you can utilize the DATE_ADD (or ADDDATE with INTERVAL) function. The syntax for this function is:

UPDATE table SET date = DATE_ADD(date, INTERVAL 1 YEAR)

Here's how it works:

  • The DATE_ADD function accepts two arguments: the date field you want to modify and the increment you want to add.
  • The interval argument is specified in the format of INTERVAL unit value. In this case, we specify 1 YEAR to add one year to the date.

Example:

Consider the following SQL statement:

UPDATE appointments SET appointment_date = DATE_ADD(appointment_date, INTERVAL 1 YEAR)

This statement would add one year to the appointment_date field in the appointments table for all rows that meet the specified conditions (e.g., where appointment_date is later than a certain date).

The above is the detailed content of How to Add One Year to a Date Value in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn