Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subtract variable number of days from timestamp in db2

Tags:

sql

db2

I know the following can be done in db2:

select * from table where created_date < current_timestamp - 5 days;

but what is the correct syntax to do something like the following?

begin
    declare numdays int default 5;

    -- some logic

    select * from table where created_date < current_timestamp - numdays days;
end;
like image 548
BX21 Avatar asked Dec 20 '25 23:12

BX21


1 Answers

The variables can be directly used in place of a constant as seen below:

create table test (dt timestamp);

begin
    declare numdays int;
    set numdays = 10;
    insert into test
    SELECT CURRENT_timestamp + numdays DAYS FROM sysibm.sysdummy1;
end ;

select current_timestamp, dt from test;

Returns

 2017-12-14 08:20:39.19063 2017-12-24 08:20:35.503779
like image 173
BX21 Avatar answered Dec 22 '25 16:12

BX21



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!