两个DateTime之间的Postgresql提取但只忽略秒

时间:2015-01-23 15:28:38

标签: postgresql datetime

两个DateTime之间的Postgresql提取,但只忽略秒(两个datetime = 00的秒)

06.09.2014 18:54:35   -  06.09.2014 18:54:35
DD.MM.YYYY HH24:MI    -  DD.MM.YYYY HH24:MI

1 个答案:

答案 0 :(得分:0)

听起来就像你正在寻找date_trunc()函数一样。

with test_data as (
  select timestamp '2015-01-01 08:00:13' as ts union all
  select timestamp '2015-01-01 08:13:27'       union all
  select timestamp '2015-01-01 09:00:27'       union all
  select timestamp '2015-01-01 09:01:42' 
)
select *, date_trunc('minute', ts) 
from test_data
where date_trunc('minute', ts) between '2015-01-01 08:00' and '2015-01-01 09:00';
ts                    date_trunc
--
2015-01-01 08:00:13   2015-01-01 08:00:00
2015-01-01 08:13:27   2015-01-01 08:13:00
2015-01-01 09:00:27   2015-01-01 09:00:00

如果您需要此类查询来使用索引,则需要在表达式date_trunc('minute', your_column_name)上创建索引。