date_sub
The date_sub function is used to decrement 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_sub function has the opposite logic to date_add.
Syntax
date_sub(string startdate, int days)
Parameters
| Parameter | Mandatory | Type | Description |
|---|---|---|---|
| start_date | Yes | DATE or STRING | The start date. Supports the following formats:
|
| days | Yes | INT | The number of days to subtract.
|
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_sub('2023-08-14 17:00:00', 2);Returns 2023-08-12 after two days are subtracted.
- Example 2
select date_sub(date'2023-08-14', -1);
Returns 2023-08-15 after one day is added.
- Example 3
select date_sub(getdate(),1);
Assuming the current time is 2023-08-14 17:00:00, returns 2023-08-13.
- Example 4
select date_sub('2023-08-14 17: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