PL/PgSQL and NULL - Mailing list pgsql-sql

From Andrew Perrin
Subject PL/PgSQL and NULL
Date
Msg-id Pine.LNX.4.21.0103111536210.6221-100000@nujoma.perrins
Whole thread Raw
Responses Re: PL/PgSQL and NULL
Re: PL/PgSQL and NULL
List pgsql-sql
Greetings-

I'm trying to write what should be a simple function that returns the
minimim of two integers. The complication is that when one of the two
integers is NULL, it should return the other; and when both are NULL, it
should return NULL.  Here's what I've written:

CREATE FUNCTION min(int4, int4)
RETURNS int4
AS 'BEGIN   IF $1 ISNULL   THENRETURN $2;   ELSE  IF $2 ISNULL THEN    RETURN $1; ELSE       IF $1 > $2      THEN
RETURN$2;      ELSE        RETURN $1;      END IF; END IF;   END IF;   END;'
 
LANGUAGE 'plpgsql';

and here's what I get:

fgdata=#  select min(10, NULL);min 
-----   
(1 row)

so it looks like, for whatever reason, it's returning NULL when it should
be returning 10. Can anyone offer advice?

Thanks.

----------------------------------------------------------------------
Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology  
Chapel Hill, North Carolina, USA - http://demog.berkeley.edu/~aperrin       aperrin@socrates.berkeley.edu -
aperrin@igc.apc.org



pgsql-sql by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: recompiling to use gnu readline?
Next
From: Peter Eisentraut
Date:
Subject: Re: PL/PgSQL and NULL