Ben Kiopp
Ben Kiopp

Reputation: 13

Postgres - Add seconds to timestamptz, value from other column

I have a column a which is a timestamptz. Also, I have a column b which is an integer, which denoted the number of seconds to be added to a. I want to do have the date of a + b.

Is this the the desired way?

select a + (b::TEXT || ' seconds')::INTERVAL from table;

It's weird that we have to manually construct a string here, I would say.

Upvotes: 1

Views: 1812

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271023

This should work:

select a + b * interval '1' second

You can multiple intervals by numbers.

Note: This can work with the single quotes around either just the number or the "1 second". These are equivalent:

select a + b * interval '1' second
select a + b * interval '1 second'

I should note that commonly intervals are used with only one time unit. So, Postgres accepts either of the above. For multiple time units, then you need a single string:

select a + interval '1 day 1 hour'

Upvotes: 4

Related Questions