gpt4 book ai didi

sql - 如何将 int (yyyymmdd) 转换为 SQL 中的时间戳?

转载 作者:行者123 更新时间:2023-11-29 12:50:43 24 4
gpt4 key购买 nike

我正在使用 Redshift,并且在表格中有一个字段作为代表一天日期的 int 类型。例如 int 是 20180215 。格式为 yyyymmdd。

我想知道使用 SQL 将此 int 转换为时间戳 (yyyy-mm-dd hh:mm:ss) 的最有效方法是什么。

最佳答案

使用to_timestamp().

select to_timestamp(20180215::text, 'YYYYMMDD')::timestamp

to_timestamp
---------------------
2018-02-15 00:00:00
(1 row)

该函数返回带时区的时间戳。结果取决于当前时区设置。您可以将结果转换为 timestamp 以跳过时区部分。

关于sql - 如何将 int (yyyymmdd) 转换为 SQL 中的时间戳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54564616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com