You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/sql-reference/20-sql-functions/05-datetime-functions/date-diff.md
+36-24Lines changed: 36 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,44 +3,56 @@ title: DATE_DIFF
3
3
---
4
4
import FunctionDescription from '@site/src/components/FunctionDescription';
5
5
6
-
<FunctionDescriptiondescription="Introduced or updated: v1.2.645"/>
6
+
<FunctionDescriptiondescription="Introduced or updated: v1.2.723"/>
7
7
8
8
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.
9
9
10
10
## Syntax
11
11
12
12
```sql
13
-
DATE_DIFF(<unit>, <start_date>, <end_date>)
13
+
DATE_DIFF(
14
+
YEAR | QUARTER | MONTH | WEEK | DAY | HOUR | MINUTE | SECOND |
|`DOW`| Day of the Week. Sunday (0) through Saturday (6). |
24
+
|`DOY`| Day of the Year. 1 through 366. |
25
+
|`EPOCH`| The number of seconds since 1970-01-01 00:00:00. |
26
+
|`ISODOW`| ISO Day of the Week. Monday (1) through Sunday (7). |
27
+
|`YEARWEEK`| The year and week number combined, following ISO 8601 (e.g., 202415). |
28
+
|`MILLENNIUM`| The millennium of the date (1 for years 1–1000, 2 for 1001–2000, etc.). |
21
29
22
30
## Examples
23
31
24
-
This example calculates the difference in hours between **yesterday**and **today**:
32
+
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:
25
33
26
34
```sql
27
-
SELECT DATE_DIFF(HOUR, YESTERDAY(), TODAY());
28
-
29
-
┌───────────────────────────────────────┐
30
-
│ DATE_DIFF(HOUR, yesterday(), today()) │
31
-
├───────────────────────────────────────┤
32
-
│ 24 │
33
-
└───────────────────────────────────────┘
35
+
SELECT
36
+
DATE_DIFF(YEAR, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_year,
37
+
DATE_DIFF(QUARTER, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_quarter,
38
+
DATE_DIFF(MONTH, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_month,
39
+
DATE_DIFF(WEEK, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_week,
40
+
DATE_DIFF(DAY, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_day,
41
+
DATE_DIFF(HOUR, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_hour,
42
+
DATE_DIFF(MINUTE, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_minute,
43
+
DATE_DIFF(SECOND, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_second,
44
+
DATE_DIFF(DOW, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_dow,
45
+
DATE_DIFF(DOY, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_doy,
46
+
DATE_DIFF(EPOCH, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_epoch,
47
+
DATE_DIFF(ISODOW, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_isodow,
48
+
DATE_DIFF(YEARWEEK, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_yearweek,
49
+
DATE_DIFF(MILLENNIUM, TIMESTAMP'2020-01-01 00:00:00', NOW()) AS diff_millennium;
34
50
```
35
51
36
-
This example calculates the difference in years between the current date and January 1, 2000;
|`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.). |
21
31
22
32
## Return Type
23
33
24
34
Integer.
25
35
26
36
## Examples
27
37
28
-
```sql
29
-
SELECT NOW();
30
-
31
-
┌────────────────────────────┐
32
-
│ now() │
33
-
├────────────────────────────┤
34
-
│ 2024-05-2202:55:52.954761 │
35
-
└────────────────────────────┘
36
-
37
-
SELECT DATE_PART(DAY, NOW());
38
-
39
-
┌───────────────────────┐
40
-
│ date_part(day, now()) │
41
-
├───────────────────────┤
42
-
│ 22 │
43
-
└───────────────────────┘
44
-
45
-
SELECT DATE_PART(DOW, NOW());
46
-
47
-
┌───────────────────────┐
48
-
│ date_part(dow, now()) │
49
-
├───────────────────────┤
50
-
│ 3 │
51
-
└───────────────────────┘
38
+
This example demonstrates how to use DATE_PART to extract various components—such as year, month, ISO week day, year-week combination, and millennium—from the current timestamp:
│ now() │ EXTRACT(DAY FROM now()) │ EXTRACT(DOY FROM now()) │ EXTRACT(EPOCH FROM now()) │ EXTRACT(ISODOW FROM now()) │ EXTRACT(YEARWEEK FROM now()) │ EXTRACT(MILLENNIUM FROM now()) │
0 commit comments