Skip site navigation (1) Skip section navigation (2)

Functions with Null Arguments?

From: Thomas Swan <tswan(at)olemiss(dot)edu>
To: pgsql-sql(at)postgresql(dot)org
Subject: Functions with Null Arguments?
Date: 2000-08-15 06:18:14
Message-ID: 5.0.0.11.2.20000815010809.01e52570@sunset.backbone.olemiss.edu (view raw or flat)
Thread:
Lists: pgsql-sql
Is there anyway way to get the following to work?

table foo
id | name1   | name2
---+---------+------
  0 | Abe     | NULL
  1 | William | Bill


create function prefname(text, text)
returns text as'
declare
         name1 alias for $1;
         name2 alias for $2;
begin
         if name2 isnull
         then
                 return name1;
         else
                 return name2;
         end if;
end;'
language 'plpgsql';

if I do
select id, name1, name2, prefname(name1, name2) as pref from foo;

I wind up with

id | name1   | name2 | prefname
---+---------+-------+-----------
  0 | Abe     | NULL  | NULL
  1 | William | Bill  | Bill

instead of

id | name1   | name2 | prefname
---+---------+-------+-----------
  0 | Abe     | NULL  | Abe
  1 | William | Bill  | Bill

I assume this has to do with Postgres not executing the function is one of 
the arguments is missing...


-
- Thomas Swan
- Graduate Student  - Computer Science
- The University of Mississippi
-
- "People can be categorized into two fundamental
- groups, those that divide people into two groups
- and those that don't."

Responses

pgsql-sql by date

Next:From: Martin DologDate: 2000-08-15 08:34:56
Subject: My Indices doesn't work
Previous:From: Jesus AneirosDate: 2000-08-14 21:26:12
Subject: RE: select an entry with a NULL date field

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group