开发者

problem with SQL query in DB2

开发者 https://www.devze.com 2023-01-05 22:05 出处:网络
How can i write a query in DB2 for following 开发者_开发技巧thing: The difference between current timestamp and a timestamp field in dB should be >=4 hours AND

How can i write a query in DB2 for following 开发者_开发技巧thing:

The difference between current timestamp and a timestamp field in dB should be >=4 hours AND <= 24 hours


You don't really give enough information to answer the question (i.e., do you want data only from the past, only in the future, etc), but let's assume you want the data where the timestamp column ("tscolumn") is more than 4 hours old and less than 24 hours old:

select * 
from   table t
where  t.tscolumn between current timestamp - 4 hours 
                      and current timestamp - 24 hours

If my assumption is wrong it's pretty easy to rewrite this to meet your requirements.


Try following

select * from tableName where 
                 date <=  DATEADD(Hour, -4, CURRENT_TIME) and 
                 date date >=  DATEADD(Hour, -24, CURRENT_TIME)


select * 
from   table t
where  timestampdiff(8,char(current timestamp - time_from_table)) between 4 and 24  

here timestamp(8, - refers hours, below are the values for different arguments.

Value Description

1   Fractions of a second
2   Seconds
4   Minutes
8   Hours
16  Days
32  Weeks
64  Months
128 Quarters
256 Years
0

精彩评论

暂无评论...
验证码 换一张
取 消