pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: José Soares <jose@sferacarta.com>
To: Anton de Wet <adw@obsidian.co.za>
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Letting a function work on NULL
Date: Mon, 21 Jun 1999 14:13:08 +0200
Message-ID: <376E2C54.9EB1CE1F@sferacarta.com> (raw)
References: <Pine.LNX.4.10.9906202049050.21065-100000@ra.obsidian.co.za>

If you are using v6.5 you can use the coalesce() function.
COALESCE returns null if and ond only its operands all evaluate to null;
otherwise it returns the value of its first nonnull operand.

hygea=> select mantello, coalesce(mantello,'null') from cani;
mantello|case
--------+----
        |null
AL      |AL
        |null
(3 rows)



Anton de Wet ha scritto:

> I have a function that returns an integer for 100*year+month for a given
> date. Currently it croaks if the date is NULL and I would like it to
> return 0 (or even NULL).
>
> Function:
>
> create function ym (date) returns int4 as 'select
> date_part(\'year\',$1)::int4*100+date_part(\'month\',$1)::int4 ' language
> 'sql';
>
> I've tried:
> create function ym (date) returns int4 as 'select
> date_part(\'year\',$1)::int4*100+date_part(\'month\',$1)::int4 where $1 is
> not null except select 0 where $1 is null' language 'sql';
>
> (and variations with 0::int4, union instead of except, and a few desparate
> others)
>
> But all gives:
>
> select ym(NULL);
> ERROR:  typeidTypeRelid: Invalid type - oid = 0
>
> Is there any way to specify an exception on the input type?
>
> Anton
>
> --------------------------------------------------------------------
> Smith's Law:
> Any sufficiently optimistic statement is indistinguishable from sarcasm.

______________________________________________________________
PostgreSQL 6.5.0 on i586-pc-linux-gnu, compiled by gcc 2.7.2.3
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Jose'

view thread (3+ messages)

Message-ID: <376E2C54.9EB1CE1F@sferacarta.com>
Permalink:  ../376E2C54.9EB1CE1F@sferacarta.com/
Also on:    postgresql.org/message-id/376E2C54.9EB1CE1F@sferacarta.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: jose@sferacarta.com, adw@obsidian.co.za
  Subject: Re: [SQL] Letting a function work on NULL
  In-Reply-To: <376E2C54.9EB1CE1F@sferacarta.com>
  References: <Pine.LNX.4.10.9906202049050.21065-100000@ra.obsidian.co.za> <376E2C54.9EB1CE1F@sferacarta.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox