|
| 1 | +--- |
| 2 | +title: DATE_BETWEEN |
| 3 | +--- |
| 4 | +import FunctionDescription from '@site/src/components/FunctionDescription'; |
| 5 | + |
| 6 | +<FunctionDescription description="Introduced or updated: v1.2.725"/> |
| 7 | + |
| 8 | +Calculates the time interval between two dates or timestamps, returning the difference as an integer in the specified unit, with positive values indicating the first time is earlier than the second, and negative values indicating the opposite. |
| 9 | + |
| 10 | +See also: [DATE_DIFF](date-diff.md) |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```sql |
| 15 | +DATE_BETWEEN( |
| 16 | + YEAR | QUARTER | MONTH | WEEK | DAY | HOUR | MINUTE | SECOND | |
| 17 | + DOW | DOY | EPOCH | ISODOW | YEARWEEK | MILLENNIUM, |
| 18 | + <start_date_or_timestamp>, |
| 19 | + <end_date_or_timestamp> |
| 20 | +) |
| 21 | +``` |
| 22 | + |
| 23 | +| Keyword | Description | |
| 24 | +|--------------|-------------------------------------------------------------------------| |
| 25 | +| `DOW` | Day of the Week. Sunday (0) through Saturday (6). | |
| 26 | +| `DOY` | Day of the Year. 1 through 366. | |
| 27 | +| `EPOCH` | The number of seconds since 1970-01-01 00:00:00. | |
| 28 | +| `ISODOW` | ISO Day of the Week. Monday (1) through Sunday (7). | |
| 29 | +| `YEARWEEK` | The year and week number combined, following ISO 8601 (e.g., 202415). | |
| 30 | +| `MILLENNIUM` | The millennium of the date (1 for years 1–1000, 2 for 1001–2000, etc.). | |
| 31 | + |
| 32 | +## DATE_DIFF vs. DATE_BETWEEN |
| 33 | + |
| 34 | +The `DATE_DIFF` function counts how many boundaries of a user-specified unit (such as day, month, or year) are crossed between two dates, while `DATE_BETWEEN` counts how many complete units fall strictly between them. For example: |
| 35 | + |
| 36 | +```sql |
| 37 | +SELECT |
| 38 | + DATE_DIFF(month, '2025-07-31', '2025-10-01'), -- returns 3 |
| 39 | + DATE_BETWEEN(month, '2025-07-31', '2025-10-01'); -- returns 2 |
| 40 | +``` |
| 41 | + |
| 42 | +In this example, `DATE_DIFF` returns `3` because the range crosses three month boundaries (July → August → September → October), while `DATE_BETWEEN` returns `2` because there are two full months between the dates: August and September. |
| 43 | + |
| 44 | +## Examples |
| 45 | + |
| 46 | +This example calculates the difference between a fixed timestamp (`2020-01-01 00:00:00`) and the current timestamp (`NOW()`), across various units such as year, ISO weekday, year-week, and millennium: |
| 47 | + |
| 48 | +```sql |
| 49 | +SELECT |
| 50 | + DATE_BETWEEN(YEAR, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_year, |
| 51 | + DATE_BETWEEN(QUARTER, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_quarter, |
| 52 | + DATE_BETWEEN(MONTH, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_month, |
| 53 | + DATE_BETWEEN(WEEK, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_week, |
| 54 | + DATE_BETWEEN(DAY, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_day, |
| 55 | + DATE_BETWEEN(HOUR, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_hour, |
| 56 | + DATE_BETWEEN(MINUTE, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_minute, |
| 57 | + DATE_BETWEEN(SECOND, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_second, |
| 58 | + DATE_BETWEEN(DOW, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_dow, |
| 59 | + DATE_BETWEEN(DOY, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_doy, |
| 60 | + DATE_BETWEEN(EPOCH, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_epoch, |
| 61 | + DATE_BETWEEN(ISODOW, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_isodow, |
| 62 | + DATE_BETWEEN(YEARWEEK, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_yearweek, |
| 63 | + DATE_BETWEEN(MILLENNIUM, TIMESTAMP '2020-01-01 00:00:00', NOW()) AS diff_millennium; |
| 64 | +``` |
| 65 | + |
| 66 | +```sql |
| 67 | +┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ |
| 68 | +│ diff_year │ diff_quarter │ diff_month │ diff_week │ diff_day │ diff_hour │ diff_minute │ diff_second │ diff_dow │ diff_doy │ diff_epoch │ diff_isodow │ diff_yearweek │ diff_millennium │ |
| 69 | +├───────────┼──────────────┼────────────┼───────────┼──────────┼───────────┼─────────────┼─────────────┼──────────┼──────────┼────────────┼─────────────┼───────────────┼─────────────────┤ |
| 70 | +│ 5 │ 21 │ 63 │ 276 │ 1933 │ 46414 │ 2784887 │ 167093234 │ 1933 │ 1933 │ 167093234 │ 1933 │ 276 │ 0 │ |
| 71 | +└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ |
| 72 | +``` |
0 commit comments