problem with SQL query in DB2

牧云@^-^@ 提交于 2019-12-12 01:35:19

问题


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


回答1:


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.




回答2:


Try following

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



回答3:


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


来源:https://stackoverflow.com/questions/3177340/problem-with-sql-query-in-db2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!