Re: Select Interval in plpgsql

From: John DeSoi <desoi(at)pgedit(dot)com>
To: "Derrick Betts" <derrick(at)grifflink(dot)com>
Cc: <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Select Interval in plpgsql
Date: 2005-03-22 05:52:16
Message-ID: 8BF0CC45-9A96-11D9-9F79-000A95B03262@pgedit.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


On Mar 21, 2005, at 10:24 PM, Derrick Betts wrote:

> I can't seem to figure out how to accomplish this task in a plpgsql
> function:
>  
> I have a timestamp variable 'apt_time' and a varchar variable
> 'time_offset'.  The time_offset variable usually looks like this: '-2
> hours'.  What I want to do is add the time_offset to the timestamp. 
> So I have tried:
>  

Here is an example:

create or replace function time_test (time_offset text)
returns timestamp as '
declare
tm timestamp = now();
adjust timestamp;
begin
adjust := tm + time_offset::interval;
return adjust;
end;
' language plpgsql;

select time_test('2 hours');
time_test
----------------------------
2005-03-22 02:45:59.357986
(1 row)

You could also write the assignment like this:

select into adjust tm + time_offset::interval;

John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tim Bowden 2005-03-22 11:44:17 Compiling 8.0.1 on Ubuntu AMD_64
Previous Message Derrick Betts 2005-03-22 03:24:21 Select Interval in plpgsql