date_add
The date_add function is used to calculate the date obtained by incrementing the start_date by the specified number of days.
To obtain a date with a specified offset from the current date, it can be used together with current_date or getdate.
Note that the date_add function has the opposite logic to date_sub.
Syntax
date_add(string startdate, int days)
Parameters
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| start_date | Yes | DATE or STRING | The start date. Supports the following formats:
|
| days | Yes | BIGINT | The number of days to add.
|
Return Values
Returns a date value of type DATE, in the format yyyy-mm-dd.
- If start_date is not of type DATE or STRING, an error is returned with the message "data type mismatch".
- If start_date is of type DATE or STRING but does not conform to the input format for date values, NULL is returned.
- If the start_date value is NULL, NULL is returned.
- If the days value is NULL, NULL is returned.
Example Code
- Example 1
select date_add('2023-02-28 00:00:00', 1);Returns 2023-03-01. Adding one day results in a date beyond the last day of February of that year, so the actual value is the first day of the next month.
- Example 2
select date_add(date '2023-02-28', -1);
Returns 2023-02-27. Subtracts one day.
- Example 3
select date_add('2023-02-28 00:00:00', 20);Returns 2023-03-20.
- Example 4
select date_add(getdate(),-1);
Assuming the current time is 2023-08-14 16:00:00, returns 2023-08-13.
- Example 5
select date_add('2023-02-28 00:00:00', null);Returns NULL.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot