tags:

views:

45

answers:

2

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

A: 

Try following

select * from tableName where 
                 date <=  DATEADD(Hour, -4, CURRENT_TIME) and 
                 date date >=  DATEADD(Hour, -24, CURRENT_TIME)
Salil
thanks for replying.But i dint get it.Can u provide some brief describtion
aks
DATEADD is not a standard function available in DB2.
Ian Bjorhovde
+1  A: 

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.

Ian Bjorhovde