Re: creating a function returning FALSE on NULL input ? - Mailing list pgsql-general

From David Johnston
Subject Re: creating a function returning FALSE on NULL input ?
Date
Msg-id 746857F9-3C13-454A-AD73-CD9AEE4DFA45@yahoo.com
Whole thread Raw
In response to creating a function returning FALSE on NULL input ?  (Yvon Thoraval <yvon.thoraval@gmail.com>)
Responses Re: creating a function returning FALSE on NULL input ?  (Yvon Thoraval <yvon.thoraval@gmail.com>)
List pgsql-general
On Nov 7, 2012, at 0:33, Yvon Thoraval <yvon.thoraval@gmail.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.

pgsql-general by date:

Previous
From: Yvon Thoraval
Date:
Subject: creating a function returning FALSE on NULL input ?
Next
From: Craig Ringer
Date:
Subject: Re: Tuning / performance questions