DATE_ADD

DATE_ADD is a function in SQL that is used to add a specified time interval to a date.

DATE_ADD(date, INTERVAL expr type)

  • date: A valid date expression. This parameter represents the starting point. MySQL will add time to this date.
  • interval expr type: A combination of two parameters. ‘expr’ is the value that you want to add. ‘type’ is the type of interval that the ‘expr’ should be interpreted as. The possible types are SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.

Example

SELECT DATE_ADD('2022-01-01', INTERVAL 5 DAY) as NewDate;

Output

+------------+
| NewDate |
+------------+
| 2022-01-06 |
+------------+

Explanation

The DATE_ADD() function is used to add a specified time interval to a date in MySQL. In the provided example, five days are added to the date ‘2022-01-01’. As the result, a new date ‘2022-01-06’ is returned.

DATE_ADD(date, INTERVAL value unit)

  • date: This is the starting point or the original date from which an interval value is to be added. The date must be of DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or TIMESTAMP WITH LOCAL TIME ZONE datatype in Oracle.
  • interval: This is the keyword used in Oracle SQL to specify that the function needs to perform an action based on a period of time.
  • value: This represents the number quantity of intervals to be added to the original date. It can be a positive or negative number.
  • unit: This indicates the unit of the interval value that needs to be added to the original date. The unit must be one of the following keywords

Example

SELECT DATE_ADD('2000-12-01', INTERVAL 1 YEAR) AS NewDate;

Output

'2001-12-01'

Explanation

In this example, the DATE_ADD function is used to add one year to the date ‘2000-12-01’. Hence, the output is ‘2001-12-01’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.