Re: how to return field based on field= NULL or not

From: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
To: Juan Backson <juanbackson(at)gmail(dot)com>
Cc: Wojtek <foo(at)twine(dot)pl>, pgsql-general(at)postgresql(dot)org
Subject: Re: how to return field based on field= NULL or not
Date: 2009-08-24 05:50:10
Message-ID: dcc563d10908232250y28679beds94fbcb7799cba72b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Aug 23, 2009 at 11:43 PM, Juan Backson<juanbackson(at)gmail(dot)com> wrote:
> Hi,
>
> Thank you for your help.
>
> What I want to dos is as follows:
>
> SELECT COALESCE(fieldA::text,fieldB||fieldC||fieldD) from ring where
> group_id = 1
>
> if fieldB is NULL, i will want it to return fieldC|| fieldD
> if fieldB and fieldC is null, I want it to return fieldD.
>
> Basically, fieldD is always going to have data, but fieldB and fieldC can be
> NULL.
>
> How can I revise the query to meet that purpose?

Just coalesce them all to '' and cat them:
... coalesce(a,'')||coalesce(b,'')||coalesce(c,'')||d ...

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2009-08-24 06:07:41 Re: Install from Source On Windows - University of Sydney Research
Previous Message Juan Backson 2009-08-24 05:43:42 Re: how to return field based on field= NULL or not