5

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.

1
  • I forgot to mention that "days" is a column in the same table, not constant. Commented May 8, 2009 at 6:54

3 Answers 3

8
select now() + cast('1 day' as interval) * 3 -- example: 3 days
Sign up to request clarification or add additional context in comments.

1 Comment

This was the actually exactly what I needed. I am not sure why this isn't part of your answer @Michael Buen
6
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

1 Comment

Thank you, this was really eye opener for what I can do with postgresql
2

calculatedDate timestamp without time zone;

calculatedDate := current_timestamp + interval '1' day * days_count; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.