SQL 如何从postgres中的查询中提取小时

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

How to extract hour from query in postgres

sqldatabasepostgresql

提问by redneb

I have timestamp in my table and i want to extract only hour from it. I search and find a extract function but unable to use as a query. Do i need to convert first timestamp in varchar and then extract hour from it? Here is my query:

我的表中有时间戳,我只想从中提取小时。我搜索并找到了提取功能,但无法用作查询。我是否需要在 varchar 中转换第一个时间戳,然后从中提取小时?这是我的查询:

select extract(hour from timestamp '2001-02-16 20:38:40') // example

actual query:

实际查询:

select extract(hour from timestamp observationtime) from smartvakt_device_report

回答by redneb

The following should work

以下应该工作

select extract(hour from observationtime) from smartvakt_device_report

回答by Mureinik

The word timezoneis redundant (read: wrong). You just need to give the column's name. E.g.:

这个词timezone是多余的(读:错误)。您只需要提供列的名称。例如:

db=> select extract(hour from observationtime) from smartvakt_device_report;

 date_part 
-----------
        19
(1 row)