Skip to content

Commit 81c0f7e

Browse files
authored
Update to-timestamp.md (#869)
1 parent ff4d244 commit 81c0f7e

File tree

1 file changed

+22
-11
lines changed
  • docs/en/sql-reference/20-sql-functions/05-datetime-functions

1 file changed

+22
-11
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: TO_TIMESTAMP
33
---
44
import FunctionDescription from '@site/src/components/FunctionDescription';
55

6-
<FunctionDescription description="Introduced or updated: v1.2.397"/>
6+
<FunctionDescription description="Introduced or updated: v1.2.538"/>
77

88
Converts an expression to a date with time.
99

@@ -51,27 +51,39 @@ Returns a timestamp in the format `YYYY-MM-DD hh:mm:ss.ffffff`:
5151
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
5252
```
5353

54-
- In the absence of timezone information in the given string, it assumes the timestamp as belonging to the timezone configured in Databend. However, when the timestamp comes along with a pattern, it is assumed to be in the UTC timezone.
54+
- In the absence of timezone information in the given string, it assumes the timestamp as belonging to the timezone configured in the current session.
5555

5656
```sql
5757
-- Set timezone to 'America/Toronto' (UTC-5:00, Eastern Standard Time)
5858
SET timezone = 'America/Toronto';
5959
60-
-- The 1st TO_TIMESTAMP interprets the timestamp without a pattern,
61-
-- assuming it belongs to the timezone configured in Databend.
62-
-- The 2nd TO_TIMESTAMP interprets the timestamp with a pattern,
63-
-- assuming it belongs to the UTC timezone.
64-
-- As a result, the timestamps are converted differently, leading to the difference in output.
6560
SELECT TO_TIMESTAMP('2022-01-02T01:12:00'), TO_TIMESTAMP('2022/01/02T01:12:00', '%Y/%m/%dT%H:%M:%S');
6661
6762
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
6863
│ to_timestamp('2022-01-02t01:12:00') │ to_timestamp('2022/01/02t01:12:00', '%y/%m/%dt%h:%m:%s') │
6964
├─────────────────────────────────────┼──────────────────────────────────────────────────────────┤
70-
│ 2022-01-02 01:12:00 │ 2022-01-01 20:12:00 │
65+
│ 2022-01-02 01:12:00 │ 2022-01-02 01:12:00 │
7166
└────────────────────────────────────────────────────────────────────────────────────────────────┘
7267
```
7368

7469
- If the given string matches this format but does not have the time part, it is automatically extended to this pattern. The padding value is 0.
70+
- If the conversion fails, an error will be returned. To avoid such errors, you can use the [TRY_TO_TIMESTAMP](try-to-timestamp.md) function.
71+
72+
```sql
73+
root@localhost:8000/default> SELECT TO_TIMESTAMP('20220102');
74+
error: APIError: ResponseError with 1006: cannot parse to type `TIMESTAMP` while evaluating function `to_timestamp('20220102')`
75+
76+
root@localhost:8000/default> SELECT TRY_TO_TIMESTAMP('20220102');
77+
78+
SELECT
79+
try_to_timestamp('20220102')
80+
81+
┌──────────────────────────────┐
82+
│ try_to_timestamp('20220102') │
83+
├──────────────────────────────┤
84+
│ NULL │
85+
└──────────────────────────────┘
86+
```
7587

7688
## Aliases
7789

@@ -148,13 +160,12 @@ SELECT TO_TIMESTAMP('2022/01/02T01:12:00-07:00', '%Y/%m/%dT%H:%M:%S%::z');
148160
│ 2022-01-02 03:12:00 │
149161
└────────────────────────────────────────────────────────────────────┘
150162
151-
-- When no timezone is provided in the input string, the time is assumed to be in UTC by default
152-
-- The provided string is converted from UTC to the current timezone ('America/Toronto')
163+
-- If no timezone is specified, the session's time zone applies.
153164
SELECT TO_TIMESTAMP('2022/01/02T01:12:00', '%Y/%m/%dT%H:%M:%S');
154165
155166
┌──────────────────────────────────────────────────────────┐
156167
│ to_timestamp('2022/01/02t01:12:00', '%y/%m/%dt%h:%m:%s') │
157168
├──────────────────────────────────────────────────────────┤
158-
│ 2022-01-01 20:12:00 │
169+
│ 2022-01-02 01:12:00 │
159170
└──────────────────────────────────────────────────────────┘
160171
```

0 commit comments

Comments
 (0)