PostgreSQL:将时间戳转换为时间 - 或仅从时间戳列中检索时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22835874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:39:49  来源:igfitidea点击:

PostgreSQL: Convert timestamp to time - or retrieve only time from timestamp column

postgresql

提问by Lars Juel Jensen

What I want is something equivalent to this:

我想要的是相当于这个的东西:

select to_time( to_timestamp ( '03.04.2014 12:34:00', 'DD.MM.YYYY HH24:MI:SS' ) )

Problem here is that to_time does not exist. I want this to result in

这里的问题是 to_time 不存在。我希望这导致

12:34:00 (without the date)

Is this possible ?

这可能吗 ?

回答by a_horse_with_no_name

select timestamp '2014-04-03 12:34:00'::time

I prefer ANSI date/timestamp literals as they are shorter to write than to_timestamp()

我更喜欢 ANSI 日期/时间戳文字,因为它们比 to_timestamp()

The above is equivalent to:

以上等价于:

select to_timestamp ('03.04.2014 12:34:00', 'DD.MM.YYYY HH24:MI:SS')::time

the ::timeis Postgres' short hand notation for casting a value.

::time是 Postgres 用于转换值的简写符号。

The following would be complete ANSI SQL if you want that:

如果需要,以下将是完整的 ANSI SQL:

select cast(timestamp '2014-04-03 12:34:00' as time)