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

From: Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl>
To: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>
Cc: Juan Backson <juanbackson(at)gmail(dot)com>, 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 10:54:31
Message-ID: 01604AA1-CDFF-4A75-ABE7-F8AE8598CFB2@solfertje.student.utwente.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 24 Aug 2009, at 7:50, Scott Marlowe wrote:

> 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.

I get the impression you mean || to mean C-style OR instead of SQL-
style concatenate? If not, Scott gave you the right solution already,
otherwise read on.

>> 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?

SELECT CASE
WHEN fieldA IS NOT NULL THEN fieldA
WHEN fieldB IS NOT NULL THEN fieldB
WHEN fieldC IS NOT NULL THEN fieldC
ELSE fieldD
END
FROM ring WHERE group_id = 1;

Alban Hertroys

--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.

!DSPAM:737,4a92716d11861465718119!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sam Mason 2009-08-24 11:26:02 Re: how to return field based on field= NULL or not
Previous Message Guillaume Lelarge 2009-08-24 09:37:34 Re: Strange "missing tables" problem