File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
docs/cn/sql-reference/20-sql-functions/05-datetime-functions Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ title : TO_TIMESTAMP_TZ
3+ ---
4+
5+ 将一个值转换为 [ ` TIMESTAMP_TZ ` ] ( ../../00-sql-reference/10-data-types/datetime.md#timestamp_tz ) ,同时保留 UTC 瞬时点和时区偏移。如果你希望在出错时返回 ` NULL ` 而不是报错,请使用 ` TRY_TO_TIMESTAMP_TZ ` 。
6+
7+ ## 语法
8+
9+ ``` sql
10+ TO_TIMESTAMP_TZ(< expr> )
11+ ```
12+
13+ ` <expr> ` 可以是 ISO-8601 格式的字符串(` YYYY-MM-DD ` 、` YYYY-MM-DDTHH:MM:SS[.fraction][±offset] ` )、` TIMESTAMP ` 或 ` DATE ` 。
14+
15+ ## 返回类型
16+
17+ ` TIMESTAMP_TZ `
18+
19+ ## 示例
20+
21+ ### 解析带显式偏移的字符串
22+
23+ ``` sql
24+ SELECT TO_TIMESTAMP_TZ(' 2021-12-20 17:01:01.000000 +0000' )::STRING AS utc_example;
25+
26+ ┌──────────────────────────────────────────┐
27+ │ utc_example │
28+ ├──────────────────────────────────────────┤
29+ │ 2021 - 12 - 20 17 :01 :01 .000000 + 0000 │
30+ └──────────────────────────────────────────┘
31+ ```
32+
33+ ### 将 TIMESTAMP 提升为带时区
34+
35+ ``` sql
36+ SELECT TO_TIMESTAMP_TZ(TO_TIMESTAMP(' 2021-12-20 17:01:01.000000' ))::STRING AS from_timestamp;
37+
38+ ┌──────────────────────────────────────────┐
39+ │ from_timestamp │
40+ ├──────────────────────────────────────────┤
41+ │ 2021 - 12 - 20 17 :01 :01 .000000 + 0000 │
42+ └──────────────────────────────────────────┘
43+ ```
44+
45+ ### 转换回 TIMESTAMP
46+
47+ ``` sql
48+ SELECT TO_TIMESTAMP(TO_TIMESTAMP_TZ(' 2021-12-20 17:01:01.000000 +0800' )) AS back_to_timestamp;
49+
50+ ┌────────────────────────┐
51+ │ back_to_timestamp │
52+ ├────────────────────────┤
53+ │ 2021 - 12 - 20T09:01 :01 │
54+ └────────────────────────┘
55+ ```
You can’t perform that action at this time.
0 commit comments