date_add Function

date_add(startDate, numDays)

Function Description

This function is used to calculate and return the date that is numDays days after the given startDate. If the calculation result exceeds the date range supported by the system, it will return null.

Parameter Description

  • startDate (date): The start date, which needs to conform to the date format.
  • numDays (int): The number of days to add, which can be a positive integer (to add days) or a negative integer (to subtract days).

Return Result

Returns a new date that is the result of adding numDays days to startDate. If the result exceeds the date range, it returns null.

Usage Example

  1. Adding days:
SELECT date_add('2020-05-31', 10); -- Returns 2020-06-10
  1. Reduce Days:
SELECT date_add('2020-06-10', -3); -- Returns 2020-06-07
  1. Year-end Calculation:
SELECT date_add('2020-12-31', 1); -- Returns 2021-01-01
  1. Exceeding the Date Range:
SELECT date_add('9999-12-31', 1); -- Returns null

Notes

  • Ensure that the startDate parameter is in a valid date format, otherwise the function may return null.
  • When numDays is negative, it indicates counting numDays days back from startDate.
  • Please be aware of the date range supported by the system; calculations beyond this range will return null.