Re: Specifying text to substitute for NULLs in selects

From: Mike Toews <mwtoews(at)sfu(dot)ca>
To: Said Ramirez <sramirez(at)vonage(dot)com>
Cc: Ivan Sergio Borgonovo <mail(at)webthatworks(dot)it>, pgsql-general(at)postgresql(dot)org
Subject: Re: Specifying text to substitute for NULLs in selects
Date: 2008-11-07 21:32:45
Message-ID: 4914B3FD.8070001@sfu.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Said Ramirez wrote:
> I think you are more after something like
>
> SELECT CASE WHEN foo IS NULL THEN 'NA' END FROM bar.
> -Said

An even simpler way to do this is using the COALESCE function:
http://www.postgresql.org/docs/current/interactive/functions-conditional.html

SELECT COALESCE(foo, 'NA') AS foo FROM bar;

will either return the value in the field(s) "foo" or 'NA' if it is
NULL. Keep in mind that you can't mix data types, like 'NaN'::text and
32.3::float in the result.

-Mike

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jason Long 2008-11-07 21:55:20 archive command Permission Denied?
Previous Message Ivan Sergio Borgonovo 2008-11-07 21:28:50 Re: Specifying text to substitute for NULLs in selects