New versions: Dev (3.15) | Latest (3.14) | 3.13 | 3.12 | 3.11

TIMESTAMPSUB

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

Subtract an interval of type java.lang.Number (number of days) or org.jooq.types.Interval (SQL interval type) from a timestamp (represented by java.sql.Timestamp).

SELECT DATE '2020-02-03 15:30:45' - INTERVAL 2 DAYS;
create.select(timestampSub(Timestamp.valueOf("2020-02-03 15:30:45"), 2)).fetch();

The result being

+---------------------+
| timestamp_subd       |
+---------------------+
| 2020-02-01 15:30:45 |
+---------------------+

Dialect support

This example using jOOQ:

timestampSub(Timestamp.valueOf("2020-02-03 15:30:45"), 2)

Translates to the following dialect specific expressions:

-- ACCESS
dateadd('d', -(2), #2020/02/03 15:30:45#)

-- ASE, SYBASE
dateadd(DAY, -(2), '2020-02-03 15:30:45.0')

-- AURORA_MYSQL, MEMSQL, MYSQL
date_add({ts '2020-02-03 15:30:45.0'}, INTERVAL -(2) DAY)

-- AURORA_POSTGRES, COCKROACHDB, POSTGRES, REDSHIFT
(TIMESTAMP '2020-02-03 15:30:45.0' + -(2) * INTERVAL '1 day')

-- CUBRID
date_add(DATETIME '2020-02-03 15:30:45.0', INTERVAL -(2) DAY)

-- DB2, HSQLDB
(TIMESTAMP '2020-02-03 15:30:45.0' - (2) day)

-- DERBY
{fn timestampadd(SQL_TSI_DAY, -(2), TIMESTAMP('2020-02-03 15:30:45.0')) }

-- FIREBIRD
dateadd(DAY, -(2), TIMESTAMP '2020-02-03 15:30:45.0')

-- H2, ORACLE, VERTICA
(TIMESTAMP '2020-02-03 15:30:45.0' - 2)

-- HANA
add_days(TIMESTAMP '2020-02-03 15:30:45.0', -(2))

-- INFORMIX
(DATETIME(2020-02-03 15:30:45.0) YEAR TO FRACTION - 2 UNITS DAY)

-- INGRES
(TIMESTAMP '2020-02-03 15:30:45.0' - DATE(2 || ' days'))

-- MARIADB
date_add(TIMESTAMP '2020-02-03 15:30:45.0', INTERVAL -(2) DAY)

-- SQLDATAWAREHOUSE, SQLSERVER
dateadd(DAY, -(2), CAST('2020-02-03 15:30:45.0' AS DATETIME2))

-- SQLITE
strftime('%Y-%m-%d %H:%M:%f', '2020-02-03 15:30:45.0', (CAST(-(2) AS varchar) || ' day'))

-- TERADATA
TIMESTAMP '2020-02-03 15:30:45.0' - CAST(2 || ' 00:00:00' AS INTERVAL DAY_TO_SECOND)

(These are currently generated with jOOQ 3.15, see #10141)

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo