This topic describes how to use the date function DATE_SUB in Realtime Compute.

Syntax

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

Input parameters

Parameter Data type
startdate VARCHAR
Note The format of a VARCHAR type date is yyyy-MM-dd or yyyy-MM-dd HH:mm:ss.
time TIMESTAMP
days INT

Description

Subtracts an interval that is specified by days from the specified date and returns a new date. The return value is a VARCHAR type date in the yyyy-MM-dd format. If an input parameter is null or a parsing error occurs, null is returned.

Example

  • Test data
    date1 (VARCHAR) nullstr (VARCHAR)
    2017-10-15 null
  • Test statements
    SELECT DATE_SUB(date1, 30) as var1,
     DATE_SUB(TIMESTAMP '2017-10-15 23:00:00',30) as var2,
     DATE_SUB(nullstr,30) as var3
    FROM T1;
  • Test results
    var1 (VARCHAR) var2 (VARCHAR) var3 (VARCHAR)
    2017-09-15 2017-09-15 null