SQL DATE_SUB function
Subtracts a number of days, or any INTERVAL unit, from a date or datetime. SUBDATE is its alias.
- Catégorie
- Dates & times
- Returns
date / datetime- In this category
- 66 of 121
Syntaxe
DATE_SUB(date, INTERVAL n unit)
SUBDATE(date, days)
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Different name or syntax | DATEADD(day, -7, d); pass a negative number |
| PostgreSQL | Different name or syntax | d - INTERVAL '7 days' |
| MySQL | Different name or syntax | See the examples and notes below. |
| SQLite | Oui | date(d, '-7 days') |
| Oracle | Different name or syntax | d - 7 or d - INTERVAL '7' DAY |
Exemples
MySQL
SELECT DATE_SUB('2026-08-01', INTERVAL 7 DAY); -- 2026-07-25
MySQL
SELECT SUBDATE('2026-08-01', 7); -- 2026-07-25
SQL Server
SELECT DATEADD(DAY, -7, '2026-08-01');
Bon à savoir
- Every engine that lacks DATE_SUB subtracts with a negative: DATEADD(day, -7, d) on SQL Server, d - 7 on Oracle, date(d, '-7 days') on SQLite.
- On PostgreSQL, date - integer also subtracts days: DATE '2026-08-10' - 7 gives DATE '2026-08-03'.
- SUBDATE with a bare number means days only; wrap units in INTERVAL.
Questions about DATE_SUB / SUBDATE
How do I subtract days in SQL Server?
There is no DATE_SUB; call DATEADD with a negative value: DATEADD(DAY, -7, d).
Does DATE_SUB handle months?
Yes, DATE_SUB('2026-03-31', INTERVAL 1 MONTH) gives 2026-02-28, clamping to the month's last day.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Get the first and last day of the month in SQL Snippet
- Get the first and last day of the year in SQL Snippet
Cheat sheets for the engines that have it: MySQL, SQL Server, PostgreSQL, 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.