Re: Questions about my ifnull function

From: "Nigel J(dot) Andrews" <nandrews(at)investsystems(dot)co(dot)uk>
To: Steve Crawford <scrawford(at)pinpointresearch(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Questions about my ifnull function
Date: 2003-09-23 20:06:48
Message-ID: Pine.LNX.4.21.0309232104400.25713-100000@ponder.fairway2k.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 23 Sep 2003, Steve Crawford wrote:

> Having a requirement to change null into a certain value in a query I
> created a couple versions of an ifnull function as follows:
>
> create or replace function "ifnull" (text, text) returns text as '
> begin
> if $1 is null
> then
> return $2;
> else
> return $1;
> end if;
> end;' language 'plpgsql';
>
> create or replace function "ifnull2" (text, text) returns text as '
> select case when $1 is null then $2 else $1 end;
> ' language 'sql';
>
> The functions work fine but I have some questions:
>
> 1. Did I overlook a better builtin function?

coalesce

3. You get your function called sometimes without error for other data types
because of implicit casting to text type.

> steve=# select ifnull(null,5::int);
> ifnull
> --------
> 5

I could have sworn int to text wasn't an implicit cast now. Damn memory.

--
Nigel J. Andrews

In response to

Browse pgsql-general by date

  From Date Subject
Next Message scott.marlowe 2003-09-23 20:10:19 Re: About GPL and proprietary software
Previous Message Steve Crawford 2003-09-23 19:58:03 Questions about my ifnull function