gpt4 book ai didi

sql - 如何从 `hh:mm:ss`中提取 `yyyy/mm/dd hh:mm:ss`

转载 作者:行者123 更新时间:2023-12-05 02:25:37 25 4
gpt4 key购买 nike

我有下表没有时区的时间戳(6)

2000/01/01 0:00:00
2000/01/01 10:00:00
2000/01/01 04:00:00

我想得到hh:mm:ss我想要的结果如下

0:00:00
10:00:00
04:00:00

有什么好的方法吗?

最佳答案

  1. to_char(col,'HH:MI:SS')如果您只是提取文本。该函数为您提供了很大的格式化灵 active ,并在几秒后截断/丢弃所有内容,不进行舍入。

  2. 要像呈现的所需结果一样修剪前导零,您可以添加 FM fill mode modifier prefixto_char() 中的所需字段。

  3. col::time(0)还将保留 time 数据类型,允许您使用足够的函数和运算符:

    time, timestamp, and interval accept an optional precision value p which specifies the number of fractional digits retained in the seconds field.

    设置为 0 它将舍入几分之一秒。它最初是由其他人建议的 - 我认为最好的选择 - 但当线程被错误地丢弃为重复时,答案被删除了。

  4. date_trunc()让您丢弃分数,同时保留适当的类型。

select now()::timestamp            as "now",
to_char(now(),'HH:MI:SS') as "to_char()",
to_char(now(),'FMHH:MI:SS') as "fillmode", --trims one leading zero
now()::time(0) as "::time(0)",--rounds fractions
date_trunc('seconds',now() )::time as "date_trunc()";--discards fractions

/* now | to_char() | fillmode | ::time(0) | date_trunc()
----------------------------+-----------+----------+-----------+--------------
2022-11-14 09:03:04.810214 | 09:03:04 | 9:03:04 | 09:03:05 | 09:03:04
(1 row)
*/

关于sql - 如何从 `hh:mm:ss`中提取 `yyyy/mm/dd hh:mm:ss`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74427638/

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