sqlpostgresqlplpgsql

How to substitute variable in postgres?


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


Solution

  • you can concat value with item variable

    SELECT CURRENT_TIMESTAMP + (item::text || ' day')::interval AS op