本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
TO_TIMESTAMP 函数
TO_TIMESTAMP 将 TIMESTAMP 字符串转换为 TIMESTAMPTZ。
语法
to_timestamp (timestamp)
to_timestamp (timestamp, format)
参数
- timestamp
-
可以转换为时间戳字符串的时间戳字符串或数据类型。
- format
-
与 Spark 的日期时间模式相匹配的字符串文字。有关有效的日期时间模式,请参阅用于格式化和解析的日期时间模式
。
返回类型
TIMESTAMP
示例
以下示例演示如何使用 TO_TIMESTAMP 函数将时间戳字符串转换为时间戳。
select current_timestamp() as timestamp, to_timestamp( current_timestamp(), 'YYYY-MM-DD HH24:MI:SS') as second;timestamp | second -------------------------- ---------------------- 2021-04-05 19:27:53.281812 | 2021-04-05 19:27:53+00
可以传递日期的 TO_TIMESTAMP 部分。其余日期部分设置为默认值。时间包括在输出中:
SELECT TO_TIMESTAMP('2017','YYYY');to_timestamp -------------------------- 2017-01-01 00:00:00+00
以下 SQL 语句将字符串 '2011-12-18 24:38:15 '转换为时间戳。结果是时间戳落在第二天,因为小时数超过 24 小时:
select to_timestamp('2011-12-18 24:38:15', 'YYYY-MM-DD HH24:MI:SS');to_timestamp ---------------------- 2011-12-19 00:38:15+00