Cast varchar to interval in plpgsql

From: "Sergei Pohilko" <spohilko(at)onepage(dot)com>
To: pgsql-general(at)postgresql(dot)org(dot)pgsql-sql(at)postgresql(dot)org
Subject: Cast varchar to interval in plpgsql
Date: 2001-06-26 23:15:45
Message-ID: 9hb55g$cj1$1@news.tht.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

Hi,

I need to create a PL/pgSQL function to add months (something like this):

CREATE
Function ADD_MONTHS
( timestamp, -- start date
int4 ) -- number of months (>0 or <0)
RETURNS timestamp -- new date
AS '
DECLARE
v_arg1 ALIAS FOR $1;
v_arg2 ALIAS FOR $2;
v_line2 varchar(100);
v_qq timestamp;
BEGIN
v_line2 := ltrim(to_char(v_arg2,''999999''),'' '')||'' month'';

select v_arg1+v_line2::interval
into v_qq;

RETURN v_qq;
END; -- Function ADD_MONTHS
' LANGUAGE 'plpgsql';
----------------------------------------

But get error message when try to call it:

>select add_months(now(),3);

>Cannot cast type 'varchar' to 'interval'

Any ideas?

Thanks.
Sergei Pohilko

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2001-06-27 00:06:23 Re: Why is NULL not indexable?
Previous Message Thalis A. Kalfigopoulos 2001-06-26 23:05:29 Re: Capitalizing First Letter of Every Word

Browse pgsql-sql by date

  From Date Subject
Next Message Craig Longman 2001-06-27 05:28:03 strange plan
Previous Message Alex Pilosov 2001-06-26 22:34:32 Re: Storing image contents in TEXT fields