convert integer to interval ?

Started by Peter Pilslabout 23 years ago4 messagesgeneral
Jump to latest
#1Peter Pilsl
pilsl@goldfisch.at

Sorry, I dont get familar with timefunctions in 7.3.2. I just
upgraded and I need a function that adds a given interval in days to a
given timestamp.

something like
select current_timestamp+interval '7 days';

in my table the 7 is given as integer. And I dont get the conversion
from this integer to interval where the integernumber is in days.

thnx,
peter

--
mag. peter pilsl
IT-Consulting
tel: +43-699-1-3574035
fax: +43-699-4-3574035
pilsl@goldfisch.at
http://www.goldfisch.at

In reply to: Peter Pilsl (#1)
Re: convert integer to interval ?

pilsl@goldfisch.at wrote:

Sorry, I dont get familar with timefunctions in 7.3.2. I just
upgraded and I need a function that adds a given interval in days to a
given timestamp.

something like
select current_timestamp+interval '7 days';

in my table the 7 is given as integer. And I dont get the conversion
from this integer to interval where the integernumber is in days.

Is there any reason why you can't do:

sprintf(buf,"select current_timestamp+interval '%d days'",int_days);

Magnus

#3Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Peter Pilsl (#1)
Re: convert integer to interval ?

On Sat, 8 Feb 2003 pilsl@goldfisch.at wrote:

Sorry, I dont get familar with timefunctions in 7.3.2. I just
upgraded and I need a function that adds a given interval in days to a
given timestamp.

something like
select current_timestamp+interval '7 days';

in my table the 7 is given as integer. And I dont get the conversion
from this integer to interval where the integernumber is in days.

One way is to make an interval string and cast it (something like)
select current_timestamp + CAST(num || ' days' AS interval);

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Magnus Naeslund(f) (#2)
Re: convert integer to interval ?

"Magnus Naeslund(f)" <mag@fbab.net> writes:

pilsl@goldfisch.at wrote:

something like
select current_timestamp+interval '7 days';

Is there any reason why you can't do:

sprintf(buf,"select current_timestamp+interval '%d days'",int_days);

Also, if you really only want the result to the nearest day, date
arithmetic (as opposed to timestamp arithmetic) works the way you want.
For example,

regression=# select current_date, current_date + 7;
date | ?column?
------------+------------
2003-02-08 | 2003-02-15
(1 row)

regards, tom lane