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

DATE_SUB

Syntax

STRING DATE_SUB(STRING startdate, INT days)
STRING DATE_SUB(TIMESTAMP time, INT days)

Description

The DATE_SUB function is used to return the date obtained by subtracting a specified number of days from a date. If the target number of days is negative, the corresponding number of days is added.

Parameter Description

  • Date: The data type is TIMESTAMP or STRING (format: yyyy-MM-dd HH:mm:ss). The value can be NULL.
  • Number of days to add: The data type is INT.
  • Return date: The date obtained by subtracting the specified number of days from the date. The data type is STRING.

Example

CREATE TABLE source (
  time1 TIMESTAMP
) WITH (
  'connector' = 'datagen',
  'rows-per-second' = '1'
);
CREATE TABLE Sink (
  date1 string,
  date2 string,
  date3 string
) with ('connector' = 'print');
INSERT into
  Sink
select
  DATE_SUB(time1,30) as date1,
  DATE_SUB('2017-09-15 00:00:00', 30) as date2,
  DATE_SUB(cast(null as timestamp),30) as date3
FROM source
Table 1 Returned Result

date1 (string)

date2 (string)

date3 (string)

2024-04-29

2017-08-16

null