Skip to content

Commit 5dc89b5

Browse files
committed
add interval functions
1 parent 5464039 commit 5dc89b5

File tree

16 files changed

+439
-1
lines changed

16 files changed

+439
-1
lines changed

docs/en/sql-reference/00-sql-reference/10-data-types/interval.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Interval
44

55
import FunctionDescription from '@site/src/components/FunctionDescription';
66

7-
<FunctionDescription description="Introduced or updated: v1.2.673"/>
7+
<FunctionDescription description="Introduced or updated: v1.2.677"/>
88

99
The INTERVAL data type represents a duration of time, allowing precise manipulation and storage of time intervals across various units.
1010

@@ -48,4 +48,34 @@ The INTERVAL data type represents a duration of time, allowing precise manipulat
4848
└───────────────────────────────────────────────────────┘
4949
```
5050
- Handles both positive and negative intervals with precision down to microseconds.
51+
- An interval can be added to or subtracted from another interval.
52+
53+
```sql title='Examples:'
54+
SELECT TO_DAYS(3) + TO_DAYS(1), TO_DAYS(3) - TO_DAYS(1);
55+
56+
┌───────────────────────────────────────────────────┐
57+
│ to_days(3) + to_days(1) │ to_days(3) - to_days(1) │
58+
├─────────────────────────┼─────────────────────────┤
59+
│ 4 days │ 2 days │
60+
└───────────────────────────────────────────────────┘
61+
```
62+
- Intervals can be added to or subtracted from DATE and TIMESTAMP values.
63+
64+
```sql title='Examples:'
65+
SELECT DATE '2024-12-20' + TO_DAYS(2), DATE '2024-12-20' - TO_DAYS(2);
66+
67+
┌───────────────────────────────────────────────────────────────────────────────────┐
68+
│ CAST('2024-12-20' AS DATE) + to_days(2) │ CAST('2024-12-20' AS DATE) - to_days(2) │
69+
├─────────────────────────────────────────┼─────────────────────────────────────────┤
70+
│ 2024-12-22 00:00:00 │ 2024-12-18 00:00:00 │
71+
└───────────────────────────────────────────────────────────────────────────────────┘
72+
73+
SELECT TIMESTAMP '2024-12-20 10:00:00' + TO_DAYS(2), TIMESTAMP '2024-12-20 10:00:00' - TO_DAYS(2);
74+
75+
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
76+
│ CAST('2024-12-20 10:00:00' AS TIMESTAMP) + to_days(2) │ CAST('2024-12-20 10:00:00' AS TIMESTAMP) - to_days(2) │
77+
├───────────────────────────────────────────────────────┼───────────────────────────────────────────────────────┤
78+
│ 2024-12-22 10:00:00 │ 2024-12-18 10:00:00 │
79+
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
80+
```
5181
- It is *not* recommended to use the MySQL client to query INTERVAL columns in Databend, as the MySQL protocol does not fully support the INTERVAL type. This may result in errors or unexpected behavior.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"label": "Interval Functions"
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: EPOCH
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.677"/>
7+
8+
Alias for [TO_SECONDS](to-seconds.md).
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Interval Functions
3+
---
4+
5+
import IndexOverviewList from '@site/src/components/IndexOverviewList';
6+
7+
This section provides reference information for the interval functions in Databend.
8+
9+
<IndexOverviewList />
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: TO_CENTURIES
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.677"/>
7+
8+
Converts a specified number of centuries into an Interval type.
9+
10+
- Accepts positive integers, zero, and negative integers as input.
11+
12+
## Syntax
13+
14+
```sql
15+
TO_CENTURIES(<centuries>)
16+
```
17+
18+
## Return Type
19+
20+
Interval (represented in years).
21+
22+
## Examples
23+
24+
```sql
25+
SELECT TO_CENTURIES(2), TO_CENTURIES(0), TO_CENTURIES(-2);
26+
27+
┌───────────────────────────────────────────────────────┐
28+
│ to_centuries(2) │ to_centuries(0) │ to_centuries(- 2) │
29+
├─────────────────┼─────────────────┼───────────────────┤
30+
200 years │ 00:00:00-200 years │
31+
└───────────────────────────────────────────────────────┘
32+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: TO_DAYS
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.677"/>
7+
8+
Converts a specified number of days into an Interval type.
9+
10+
- Accepts positive integers, zero, and negative integers as input.
11+
12+
## Syntax
13+
14+
```sql
15+
TO_DAYS(<days>)
16+
```
17+
18+
## Return Type
19+
20+
Interval (represented in days).
21+
22+
## Examples
23+
24+
```sql
25+
SELECT TO_DAYS(2), TO_DAYS(0), TO_DAYS(-2);
26+
27+
┌────────────────────────────────────────┐
28+
│ to_days(2) │ to_days(0) │ to_days(- 2) │
29+
├────────────┼────────────┼──────────────┤
30+
2 days │ 00:00:00-2 days │
31+
└────────────────────────────────────────┘
32+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: TO_DECADES
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.677"/>
7+
8+
Converts a specified number of decades into an Interval type.
9+
10+
- Accepts positive integers, zero, and negative integers as input.
11+
12+
## Syntax
13+
14+
```sql
15+
TO_DECADES(<decades>)
16+
```
17+
18+
## Return Type
19+
20+
Interval (represented in years).
21+
22+
## Examples
23+
24+
```sql
25+
SELECT TO_DECADES(2), TO_DECADES(0), TO_DECADES((- 2));
26+
27+
┌─────────────────────────────────────────────────┐
28+
│ to_decades(2) │ to_decades(0) │ to_decades(- 2) │
29+
├───────────────┼───────────────┼─────────────────┤
30+
20 years │ 00:00:00-20 years │
31+
└─────────────────────────────────────────────────┘
32+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: TO_HOURS
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.677"/>
7+
8+
Converts a specified number of hours into an Interval type.
9+
10+
- Accepts positive integers, zero, and negative integers as input.
11+
12+
## Syntax
13+
14+
```sql
15+
TO_HOURS(<hours>)
16+
```
17+
18+
## Return Type
19+
20+
Interval (in the format `hh:mm:ss`).
21+
22+
## Examples
23+
24+
```sql
25+
SELECT TO_HOURS(2), TO_HOURS(0), TO_HOURS((- 2));
26+
27+
┌───────────────────────────────────────────┐
28+
│ to_hours(2) │ to_hours(0) │ to_hours(- 2) │
29+
├─────────────┼─────────────┼───────────────┤
30+
2:00:0000:00:00-2:00:00
31+
└───────────────────────────────────────────┘
32+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: TO_MICROSECONDS
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.677"/>
7+
8+
Converts a specified number of microseconds into an Interval type.
9+
10+
- Accepts positive integers, zero, and negative integers as input.
11+
12+
## Syntax
13+
14+
```sql
15+
TO_MICROSECONDS(<microseconds>)
16+
```
17+
18+
## Return Type
19+
20+
Interval (in the format `hh:mm:ss.sssssss`).
21+
22+
## Examples
23+
24+
```sql
25+
SELECT TO_MICROSECONDS(2), TO_MICROSECONDS(0), TO_MICROSECONDS((- 2));
26+
27+
┌────────────────────────────────────────────────────────────────┐
28+
│ to_microseconds(2) │ to_microseconds(0) │ to_microseconds(- 2) │
29+
├────────────────────┼────────────────────┼──────────────────────┤
30+
0:00:00.00000200:00:00-0:00:00.000002
31+
└────────────────────────────────────────────────────────────────┘
32+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: TO_MILLENNIA
3+
---
4+
import FunctionDescription from '@site/src/components/FunctionDescription';
5+
6+
<FunctionDescription description="Introduced or updated: v1.2.677"/>
7+
8+
Converts a specified number of millennia into an Interval type.
9+
10+
- Accepts positive integers, zero, and negative integers as input.
11+
12+
## Syntax
13+
14+
```sql
15+
TO_MILLENNIA(<millennia>)
16+
```
17+
18+
## Return Type
19+
20+
Interval (represented in years).
21+
22+
## Examples
23+
24+
```sql
25+
SELECT TO_MILLENNIA(2), TO_MILLENNIA(0), TO_MILLENNIA((- 2));
26+
27+
┌───────────────────────────────────────────────────────┐
28+
│ to_millennia(2) │ to_millennia(0) │ to_millennia(- 2) │
29+
├─────────────────┼─────────────────┼───────────────────┤
30+
2000 years │ 00:00:00-2000 years │
31+
└───────────────────────────────────────────────────────┘
32+
```

0 commit comments

Comments
 (0)