SQL SUBTIME function
Subtracts a time span from a datetime or another time, the mirror of ADDTIME.
- Returns
datetime / time- In this category
- 74 of 121
语法
SUBTIME(dt, time_value)
引擎支持
| 引擎 | Support | 备注 |
|---|---|---|
| SQL Server | Different name or syntax | DATEADD with negative parts |
| PostgreSQL | Different name or syntax | dt - INTERVAL '2 hours 30 minutes' |
| MySQL | 是 | Supported under this name. |
| SQLite | Different name or syntax | datetime(d, '-2 hours') |
| Oracle | Different name or syntax | dt - INTERVAL '2:30' HOUR TO MINUTE |
示例
MySQL
SELECT SUBTIME('2026-08-01 01:00:00', '02:30:00'); -- 2026-07-31 22:30:00
PostgreSQL
SELECT '2026-08-01 01:00:00'::timestamp - INTERVAL '2:30';
值得了解
- Like ADDTIME, the span is a TIME value ('02:30:00', or '1 02:30:00' with a day part); months and years need DATE_SUB with an INTERVAL.
- SUBTIME('2026-08-01 01:00:00', '02:30:00') correctly rolls back to 2026-07-31 22:30:00 when the first argument is a datetime.
- There is no DATE_SUB-style bare-day shortcut here; use DATE_SUB for day granularity.
Questions about SUBTIME
SUBTIME vs DATE_SUB?
SUBTIME takes a TIME span like '02:30:00'; DATE_SUB takes INTERVAL unit terms and bare day counts. Use SUBTIME for sub-day precision on the clock.
What is the Oracle equivalent?
d - INTERVAL '0 02:30:00' DAY TO SECOND, or plain subtraction with a fraction of a day: d - 2.5/24.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.