有没有办法在 postgresql 中将文本转换为时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14333043/
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-10-21 00:43:48  来源:igfitidea点击:

Is there any way convert text to time in postgresql?

postgresqltime

提问by Edward

Is there any way convert text to time type? Code in postgresql as below:

有没有办法将文本转换为时间类型?postgresql 中的代码如下:

to_char(localtimestamp(0),'HH24:mi:SS')

this way I will get the value like 15:15:20,but this typeis varchar(or text).
How can I do to get the value in timetype?

这样我会得到像这样的值15:15:20,但这type是varchar(或文本)。
我该怎么做才能获得time类型的值?

Thanks!

谢谢!

回答by Quassnoi

SELECT  TO_TIMESTAMP('15:15:20', 'HH24:MI:SS')::TIME

Note that your very query (which returns local time) can be rewritten as

请注意,您的查询(返回本地时间)可以重写为

SELECT  LOCALTIME

which would return TIME

哪个会返回 TIME