Thread: convert integer to interval ?

convert integer to interval ?

From
pilsl@goldfisch.at
Date:
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

Re: convert integer to interval ?

From
"Magnus Naeslund(f)"
Date:
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


Re: convert integer to interval ?

From
Stephan Szabo
Date:
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);


Re: convert integer to interval ?

From
Tom Lane
Date:
"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