New versions: Dev (3.15) | Latest (3.14) | 3.13 | 3.12 | 3.11 | 3.10 | 3.9 | 3.8 | Old versions: 3.7 | 3.6

DATESUB

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 date (represented by java.sql.Date).

SELECT DATE '2020-02-03' - 2;
create.select(dateSub(Date.valueOf("2020-02-03"), 2)).fetch();

The result being

+------------+
| date_sub   |
+------------+
| 2020-02-01 |
+------------+

Dialect support

This example using jOOQ:

dateSub(Date.valueOf("2020-02-03"), 2)

Translates to the following dialect specific expressions:

-- ACCESS
dateadd('d', -(2), #2020/02/03#)

-- ASE, SYBASE
dateadd(DAY, -(2), '2020-02-03')

-- AURORA_MYSQL, MEMSQL, MYSQL
date_add({d '2020-02-03'}, INTERVAL -(2) DAY)

-- AURORA_POSTGRES, COCKROACHDB, POSTGRES, REDSHIFT
(DATE '2020-02-03' + -(2))

-- CUBRID, MARIADB
date_add(DATE '2020-02-03', INTERVAL -(2) DAY)

-- DB2, HSQLDB
(DATE '2020-02-03' - (2) day)

-- DERBY
CAST({fn timestampadd(SQL_TSI_DAY, -(2), DATE('2020-02-03')) } AS DATE)

-- FIREBIRD
dateadd(DAY, -(2), DATE '2020-02-03')

-- H2, ORACLE, VERTICA
(DATE '2020-02-03' - 2)

-- HANA
add_days(DATE '2020-02-03', -(2))

-- INFORMIX
(DATETIME(2020-02-03) YEAR TO DAY - 2 UNITS DAY)

-- INGRES
(DATE '2020-02-03' - DATE(2 || ' days'))

-- SQLDATAWAREHOUSE, SQLSERVER
dateadd(DAY, -(2), CAST('2020-02-03' AS date))

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

-- TERADATA
DATE '2020-02-03' - CAST(2 || ' 00:00:00' AS INTERVAL DAY_TO_SECOND)

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

The jOOQ Logo