SQL date minus date
Plain date subtraction gives an integer day count where it is supported, the cleanest way to answer 'how many days apart'.
- 카테고리
- Dates & times
- Returns
integer (days)- In this category
- 78 of 121
구문
date1 - date2
엔진 지원
| 엔진 | Support | 메모 |
|---|---|---|
| SQL Server | Different name or syntax | DATEDIFF(DAY, a, b); counts midnight boundaries |
| PostgreSQL | 예 | Supported under this name. |
| MySQL | Different name or syntax | DATEDIFF(a, b); genuinely whole days here |
| SQLite | Different name or syntax | JULIANDAY(a) - JULIANDAY(b) |
| Oracle | 예 | Supported under this name. |
예시
PostgreSQL
SELECT DATE '2026-08-01' - DATE '2026-07-01'; -- 31
Oracle
SELECT TRUNC(DATE '2026-08-01') - TRUNC(DATE '2026-07-01') FROM dual; -- 31
MySQL
SELECT DATEDIFF('2026-08-01', '2026-07-01'); -- 31 (days only)
알아 두면 좋은 정보
- SQL Server's DATEDIFF(DAY, '2026-07-31 23:00', '2026-08-01 01:00') is 1 (two midnights crossed), even though only two hours elapsed.
- Oracle DATE - DATE returns a NUMBER with a fraction if times are present; wrap in TRUNC or CAST for whole days.
- MySQL does not subtract dates with the minus sign: DATE '2026-08-01' - DATE '2026-07-01' returns 100 (20260801 - 20260701). Use DATEDIFF there, and JULIANDAY on SQLite, where string minus string is numeric too.
Questions about DATE - DATE
Is date - date the same as DATEDIFF?
On PostgreSQL and Oracle yes, in days. On SQL Server DATEDIFF counts boundaries, so hours around midnight change the answer, and date - date is an operand type error.
How do I get business days between dates?
None of the engines do it natively: count weekdays with a recursive CTE, a calendar table, or generate_series + EXTRACT(ISODOW) filters on PostgreSQL.
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.