Re: creating a function returning FALSE on NULL input ?

From: Yvon Thoraval <yvon(dot)thoraval(at)gmail(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: creating a function returning FALSE on NULL input ?
Date: 2012-11-07 06:51:23
Message-ID: CAG6bkBwoS7pO+MhN3CJfZt+NUw-fYxOoAPoSxzoY2QG0RJ=-hw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Fine thanks,I've found that way :
recettes=> DROP FUNCTION has_infos(text);
DROP FUNCTION

recettes=> CREATE FUNCTION has_infos(text DEFAULT '') RETURNS boolean AS
$BODY$
recettes$> BEGIN
recettes$> IF character_length($1) > 0 THEN
recettes$> RETURN TRUE;
recettes$> ELSE
recettes$> RETURN FALSE;
recettes$> END IF ;
recettes$> END
recettes$> $BODY$
recettes-> LANGUAGE 'plpgsql' ;
CREATE FUNCTION

I've verified, it works on NULL input...

2012/11/7 David Johnston <polobo(at)yahoo(dot)com>

> On Nov 7, 2012, at 0:33, Yvon Thoraval <yvon(dot)thoraval(at)gmail(dot)com> wrote:
>
> I'd like to create a function :
> returning true if the length of the text arg is greater than 0 ;
> false otherwise ;
>
> and also returning false when arg is NULL, then i wrote :
>
> CREATE FUNCTION has_infos(text) RETURNS boolean AS 'select
> character_length($1) > 0;' LANGUAGE SQL IMMUTABLE RETURNS FALSE ON NULL
> INPUT;
>
> where i get the error :
>
> ERROR: syntax error at or near "FALSE"
> LINE 1: ...r_length($1) > 0;' LANGUAGE SQL IMMUTABLE RETURNS FALSE ON N...
>
> with "RETURNS NULL on NULL INPUT", that's OK.
>
> --
> Yvon
>
>
> You have to allow for null input and deal with it in the function body.
> Usually via a CASE structure.
>
> David J.
>
>

--
Yvon

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-11-07 07:24:32 Re: creating a function returning FALSE on NULL input ?
Previous Message Craig Ringer 2012-11-07 06:05:06 Re: Tuning / performance questions