Updated on 2026-11-06 GMT+08:00

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

Table 1 Parameter descriptions

Parameter

Mandatory

Type

Description

start_date

Yes

DATE

or

STRING

The start date.

Supports the following formats:

  • yyyy-mm-dd
  • yyyy-mm-dd hh:mi:ss
  • yyyy-mm-dd hh:mi:ss.ff3

days

Yes

INT

The number of days to subtract.

  • When days is greater than 0, the specified number of days is subtracted from start_date.
  • When days is less than 0, the specified number of days is added to start_date.
  • When days equals 0, 0 days are added and the date remains unchanged.
  • When the days value is NULL, NULL is returned.

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.