|
1 | 1 | ---
|
2 | 2 | title: DATE_ADD
|
3 | 3 | ---
|
| 4 | +import FunctionDescription from '@site/src/components/FunctionDescription'; |
4 | 5 |
|
5 |
| -Add the time interval or date interval to the provided date or date with time (timestamp/datetime). |
| 6 | +<FunctionDescription description="Introduced or updated: v1.2.641"/> |
| 7 | + |
| 8 | +Adds a specified time interval to a DATE or TIMESTAMP value. |
6 | 9 |
|
7 | 10 | ## Syntax
|
8 | 11 |
|
9 | 12 | ```sql
|
10 |
| -DATE_ADD(<unit>, <value>, <date_or_time_expr>) |
| 13 | +DATE_ADD(<unit>, <interval>, <date_or_time_expr>) |
11 | 14 | ```
|
12 |
| -## Arguments |
13 | 15 |
|
14 |
| -| Arguments | Description | |
15 |
| -|-----------------------|-------------------------------------------------------------------------------------------------------------------| |
16 |
| -| `<unit>` | Must be of the following values: `YEAR`, `QUARTER`, `MONTH`, `DAY`, `HOUR`, `MINUTE` and `SECOND` | |
17 |
| -| `<value>` | This is the number of units of time that you want to add. For example, if you want to add 2 days, this will be 2. | |
18 |
| -| `<date_or_time_expr>` | A value of `DATE` or `TIMESTAMP` type | |
| 16 | +| Parameter | Description | |
| 17 | +|-----------------------|----------------------------------------------------------------------------------------------------| |
| 18 | +| `<unit>` | Specifies the time unit: `YEAR`, `QUARTER`, `MONTH`, `WEEK`, `DAY`, `HOUR`, `MINUTE` and `SECOND`. | |
| 19 | +| `<interval>` | The interval to add, e.g., 2 for 2 days if the unit is `DAY`. | |
| 20 | +| `<date_or_time_expr>` | A value of `DATE` or `TIMESTAMP` type. | |
19 | 21 |
|
20 | 22 | ## Return Type
|
21 | 23 |
|
22 |
| -The function returns a value of the same type as the `<date_or_time_expr>` argument. |
| 24 | +DATE or TIMESTAMP (depending on the type of `<date_or_time_expr>`). |
23 | 25 |
|
24 | 26 | ## Examples
|
25 | 27 |
|
26 |
| -Query: |
| 28 | +This example adds different time intervals (year, quarter, month, week, and day) to the current date: |
| 29 | + |
27 | 30 | ```sql
|
28 |
| -SELECT date_add(YEAR, 1, to_date('2018-01-02')); |
29 |
| - |
30 |
| -┌──────────────────────────────────────────┐ |
31 |
| -│ date_add(year, 1, to_date('2018-01-02')) │ |
32 |
| -│ Date │ |
33 |
| -├──────────────────────────────────────────┤ |
34 |
| -│ 2019-01-02 │ |
35 |
| -└──────────────────────────────────────────┘ |
| 31 | +SELECT |
| 32 | + TODAY(), |
| 33 | + DATE_ADD(YEAR, 1, TODAY()), |
| 34 | + DATE_ADD(QUARTER, 1, TODAY()), |
| 35 | + DATE_ADD(MONTH, 1, TODAY()), |
| 36 | + DATE_ADD(WEEK, 1, TODAY()), |
| 37 | + DATE_ADD(DAY, 1, TODAY()); |
| 38 | + |
| 39 | +-[ RECORD 1 ]----------------------------------- |
| 40 | + today(): 2024-10-10 |
| 41 | + DATE_ADD(YEAR, 1, today()): 2025-10-10 |
| 42 | +DATE_ADD(QUARTER, 1, today()): 2025-01-10 |
| 43 | + DATE_ADD(MONTH, 1, today()): 2024-11-10 |
| 44 | + DATE_ADD(WEEK, 1, today()): 2024-10-17 |
| 45 | + DATE_ADD(DAY, 1, today()): 2024-10-11 |
36 | 46 | ```
|
| 47 | + |
| 48 | +This example adds different time intervals (hour, minute, and second) to the current timestamp: |
| 49 | + |
| 50 | +```sql |
| 51 | +SELECT |
| 52 | + NOW(), |
| 53 | + DATE_ADD(HOUR, 1, NOW()), |
| 54 | + DATE_ADD(MINUTE, 1, NOW()), |
| 55 | + DATE_ADD(SECOND, 1, NOW()); |
| 56 | + |
| 57 | +-[ RECORD 1 ]----------------------------------- |
| 58 | + now(): 2024-10-10 01:35:33.601312 |
| 59 | + DATE_ADD(HOUR, 1, now()): 2024-10-10 02:35:33.601312 |
| 60 | +DATE_ADD(MINUTE, 1, now()): 2024-10-10 01:36:33.601312 |
| 61 | +DATE_ADD(SECOND, 1, now()): 2024-10-10 01:35:34.601312 |
| 62 | +``` |
0 commit comments