SQL Date Function DATE_ADD()
The DATE_ADD()
function is a built-in date function in SQL that adds a specified interval to a given date. It returns a new date that is the result of adding the specified interval to the input date.
Here's the syntax of the DATE_ADD()
function:
DATE_ADD(date, INTERVAL value unit)
The date
parameter specifies the input date, and the value
parameter specifies the number of units of time to add to the input date. The unit
parameter specifies the unit of time to add, and can be one of the following values:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH
Here's an example of how to use the DATE_ADD()
function in a SQL query to add 3 months to a given date:
SELECT DATE_ADD('2022-03-01', INTERVAL 3 MONTH);
This query will return a new date that is 3 months after the input date, like this:
2022-06-01
You can also use the DATE_ADD()
function in an UPDATE
statement to update a date column by adding a specified interval to each row, like this:
UPDATE my_table SET date_column = DATE_ADD(date_column, INTERVAL 1 DAY) WHERE id = 123;
This query will update the date_column
in the row with id
of 123
by adding 1 day to the existing date.
Note that the DATE_ADD()
function may have slightly different syntax and behavior depending on the specific SQL database system being used.