简体   繁体   English

在IBM DB2中,如何过滤以获取一天中特定时间之间的数据

[英]How do you filter to get data that is in between a certain time of day in IBM DB2

I am trying to add a filter condition in the DB2 database. 我试图在DB2数据库中添加过滤条件。 I am new to it and come from an Oracle background. 我是新手,来自Oracle背景。 I am trying to get records with dates in between today at 4 AM and today at 5 PM only. 我正在尝试获取日期介于今天凌晨4点和今天凌晨5点之间的记录。 I currently have the below query that returns zero results: 我目前有以下查询,返回零结果:

db2 => select datetimeColumn from datetimeExample WHERE datetimeColumn  BETWEEN timestamp(current date) - 1 day + 4 hour AND timestamp(current date) - 1 day + 13 hour

DATETIMECOLUMN
--------------------------

0 record(s) selected.

And here is the data in the table that I believe should show but there is something wrong with condition statement, any help is appreciated 这是我认为应该显示的表中数据,但条件语句有问题,我们将提供任何帮助

db2 => select * from datetimeExample

DATETIMECOLUMN
--------------------------
2016-06-16-09.38.53.759000
1988-12-25-17.12.30.000000
2016-12-25-17.10.30.000000
2016-06-16-04.10.30.000000
2016-06-16-05.10.30.000000
1988-12-25-15.12.30.000000
1988-12-25-14.12.30.000000
2016-06-16-12.10.30.000000
2016-06-16-07.10.30.000000
2016-06-16-08.10.30.000000

10 record(s) selected.

The query should work when you leave out the - 1 day . 当您省略- 1 day该查询应该起作用。 The reason is that timestamp(current date) returns the timestamp for today at zero hours. 原因是timestamp(current date)返回今天零时的时间戳。 Then you add 4 hours and are at the required start time. 然后,您增加了4个小时,并处于所需的开始时间。 Similar maths for the end time (and 5 pm should be + 17 hours ). 结束时间的数学运算类似(下午5点应为+ 17 hours )。

select datetimeColumn from datetimeExample
WHERE datetimeColumn  
BETWEEN timestamp(current date) + 4 hours AND timestamp(current date) + 17 hours

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM