Skip to content

Commit 992f7da

Browse files
authored
updates (#1846)
1 parent 1394308 commit 992f7da

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
5216327619334641427848871670932341933193316709323419332760
71+
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
72+
```

docs/en/sql-reference/20-sql-functions/05-datetime-functions/date-diff.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import FunctionDescription from '@site/src/components/FunctionDescription';
77

88
Calculates the difference between two dates or timestamps based on a specified time unit. The result is positive if the `<end_date>` is after the `<start_date>`, and negative if it's before.
99

10+
See also: [DATE_BETWEEN](date-between.md)
11+
1012
## Syntax
1113

1214
```sql
@@ -27,6 +29,18 @@ DATE_DIFF(
2729
| `YEARWEEK` | The year and week number combined, following ISO 8601 (e.g., 202415). |
2830
| `MILLENNIUM` | The millennium of the date (1 for years 1–1000, 2 for 1001–2000, etc.). |
2931

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+
3044
## Examples
3145

3246
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:

docs/en/sql-reference/20-sql-functions/05-datetime-functions/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ This section provides reference information for the datetime-related functions i
5454
- [ADD INTERVAL](addinterval.md)
5555
- [DATE_ADD](date-add)
5656
- [DATE_SUB](date-sub)
57+
- [DATE_DIFF](date-diff)
58+
- [DATE_BETWEEN](date-between.md)
5759
- [SUBTRACT INTERVAL](subtractinterval.md)
5860
- [MONTHS_BETWEEN](months-between.md)
5961
- [LAST_DAY](last-day.md)
@@ -64,7 +66,6 @@ This section provides reference information for the datetime-related functions i
6466
## Date Information Functions
6567

6668
- [DATE_PART](date-part.md)
67-
- [DATE_DIFF](date-diff)
6869
- [DATE_FORMAT](date-format)
6970
- [DATE_TRUNC](date-trunc)
7071
- [NOW](now.md)

0 commit comments

Comments
 (0)