ADD_YEARS

add_years(startDate, numYears)

Description

This function is used to calculate and return the date after adding (or subtracting) a specified number of years (numYears) to a given start date (startDate). If the calculated result exceeds the date range supported by the system, it will return null.

Parameter Description

  • startDate: Type date, represents the start date for the calculation.
  • numYears: Type int, represents the number of years to add or subtract, can be positive or negative.

Return Result

Returns a date type date, representing the calculated result.

Usage Example

  1. Calculate a future date:
SELECT add_years('2020-05-31', 3);
+----------------------------+
| add_years('2020-05-31', 3) |
+----------------------------+
| 2023-05-31                 |
+----------------------------+
2. Calculate past dates:
SELECT add_years('2020-05-31', -5);
+-----------------------------+
| add_years('2020-05-31', -5) |
+-----------------------------+
| 2015-05-31                  |
+-----------------------------+
  1. When the added years cause the result to exceed the system date range, return null:
SELECT add_years('1900-01-01', 200);
+------------------------------+
| add_years('1900-01-01', 200) |
+------------------------------+
| 2100-01-01                   |
+------------------------------+1

Precautions

  • Ensure that the input startDate parameter is in a valid date format, otherwise it may lead to inaccurate calculation results.
  • When numYears is a negative number, it indicates calculating the corresponding number of years backward from the start date.
  • If the calculation result exceeds the date range supported by the system, the function will return null. Please handle this situation accordingly.