Re: Test text value as interval

From: Robert Fitzpatrick <lists(at)webtent(dot)net>
To: Jeff Davis <pgsql(at)j-davis(dot)com>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: Test text value as interval
Date: 2008-02-08 01:59:18
Message-ID: 1202435958.8707.52.camel@columbus.webtent.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 2008-02-07 at 16:58 -0800, Jeff Davis wrote:
> On Thu, 2008-02-07 at 19:37 -0500, Robert Fitzpatrick wrote:
> > Been searching for a way to do this, but haven't found what I was hoping
> > to find. Is there any way in pl/pgsql to test a text value to see if it
> > would be a valid interval without having to try and store in a field? In
> > a trigger, I'd like to test a NEW text type field. Right now, I have
> > just the following to generate an error...
> >
> > test := NEW.textfield::interval;
> >
> > I'd like to test the field and RAISE EXCEPTION if not valid interval.
>
> Trap the error and do what you want with it:
>
> http://www.postgresql.org/docs/8.3/static/plpgsql-control-
> structures.html#PLPGSQL-ERROR-TRAPPING
>

Yes, this looks like it might work, thanks! But not sure which condition
to look for or if I'm doing this correctly. I tried syntax_error
condition, but I'm still receiving the same cast error trying this in a
trigger function...

begin
begin
test := NEW.textfield::interval;
EXCEPTION
WHEN syntax_error THEN
RAISE NOTICE 'Invalid Duration';
return null;
end;
<snip other code>
return new;
end;

> Although: why do you want to generate your own error? It seems like it
> would probably be about the same as the error produced by the casting
> failure.

My application will display whatever I can return via raise exception,
hence, that's why I'm trying this. Looking for a way to translate to the
user...

update events set event_duration = '3ho' where event_id = 2;
ERROR: invalid input syntax for type interval: "3ho"

--
Robert

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-02-08 02:13:49 Re: Test text value as interval
Previous Message Satoru Fukushima 2008-02-08 01:43:43 How can exe files such as 'pg_dump' be called from stored functions?