views:

861

answers:

2

I want update a column by adding days to current time. In pseudosyntax it would be:

UPDATE foo
SET time = current_timestamp + days::integer

days is a column in the same table.

+2  A: 
select now() + cast('1 day' as interval) * 3 -- example: 3 days
Michael Buen
+2  A: 
create function add_days_to_timestamp(t timestamptz, d int) 
returns timestamptz
as
$$
begin
    return t + interval '1' day * d;
end; 
$$ language 'plpgsql';


create operator + (leftarg = timestamptz, rightarg = int, 
         procedure = add_days_to_timestamp);

Now this would work:

update foo set time = current_timestamp + 3 /* day variable here, 
or a column from your table */

Note:

for some reason, adding an integer to date is built-in in Postgres, this would work:

select current_timestamp::date + 3 -- but only a date

this would not(unless you define your own operator, see above):

select current_timestamp + 3
Michael Buen
Thank you, this was really eye opener for what I can do with postgresql
egaga