skip to Main Content

Hi I am working in Postgres. I have below code

DO $$
DECLARE
    item integer;
begin
    item:=5;
    DROP TABLE IF EXISTS temp_data;
    CREATE temp TABLE temp_data AS
select current_timestamp + interval '1 day' as op;
 
END $$;
select * from temp_data;

In the above code in the Text ‘1 Day’ I would like to replace 1 with variable item. Can someone please help me? Thanks

2

Answers


  1. you can concat value with item variable

    SELECT CURRENT_TIMESTAMP + (item::text || ' day')::interval AS op
    
    Login or Signup to reply.
  2. Intervals overload the multiplication operator with DOUBLE PRECISION (that INT can be autocast to):

    DO
    $$
    DECLARE
        item integer;
    begin
        item:=5;
        DROP TABLE IF EXISTS temp_data;
        CREATE temp TABLE temp_data AS
        select current_timestamp + item * interval '1 day' as op;
    
    END
    $$;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search