pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: David Gauthier <davegauthierpg@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: How to get CASE statement to recognize null ?
Date: Wed, 10 Mar 2021 21:17:46 +0100
Message-ID: <F24437A8-23F9-4DAC-86F6-84A4F1045F5C@gmail.com> (raw)
In-Reply-To: <CAMBRECCzpy0xek+=GdHu3H4N3jB4rT7D5gQZuDDssqYgeFsgrw@mail.gmail.com>
References: <CAMBRECCXKr4x8NOtCrumAet80_uDGq_PZ45vHf7Qa8YB7+ES5A@mail.gmail.com>
	<2188918.1615405613@sss.pgh.pa.us>
	<CAMBRECCzpy0xek+=GdHu3H4N3jB4rT7D5gQZuDDssqYgeFsgrw@mail.gmail.com>


> On 10 Mar 2021, at 21:00, David Gauthier <davegauthierpg@gmail.com> wrote:
> 
> Hey, Thanks Tom !
> 
> It's actually a little more complicated than the simple example, so I'm not sure it can be shoehorned into coalesce...
> 
>      CASE 
>        WHEN sr.nightly_cl_display_suffix is null THEN cast (d.p4_changelist as varchar)
>      ELSE
>        cast (d.p4_changelist as varchar)||'-'||sr.nightly_cl_display_suffix
>      END as changelist

Sure it can:
	cast(d.p4_changelist as varchar) || coalesce('-'||sr.nightly_cl_display_suffix, '')

> On Wed, Mar 10, 2021 at 2:46 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> David Gauthier <davegauthierpg@gmail.com> writes:
> >     dvdb=# select
> >       CASE col1
> >         WHEN null THEN 'z'
> >         ELSE col1
> >        END as col1,
> >        col2
> >      from foo;
> 
> This test is equivalent to "col1 = null" which will always fail.
> You could try something like
> 
>         CASE WHEN col1 IS NULL THEN ... ELSE ... END
> 
> Although I think the particular thing you're doing here would
> be better solved with COALESCE(col1, 'z').
> 
>                         regards, tom lane

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






view thread (4+ messages)

Message-ID: <F24437A8-23F9-4DAC-86F6-84A4F1045F5C@gmail.com>
Permalink:  ../F24437A8-23F9-4DAC-86F6-84A4F1045F5C@gmail.com/
Also on:    postgresql.org/message-id/F24437A8-23F9-4DAC-86F6-84A4F1045F5C@gmail.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-general@postgresql.org
  Cc: haramrae@gmail.com, davegauthierpg@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: How to get CASE statement to recognize null ?
  In-Reply-To: <F24437A8-23F9-4DAC-86F6-84A4F1045F5C@gmail.com>
  References: <CAMBRECCXKr4x8NOtCrumAet80_uDGq_PZ45vHf7Qa8YB7+ES5A@mail.gmail.com> <2188918.1615405613@sss.pgh.pa.us> <CAMBRECCzpy0xek+=GdHu3H4N3jB4rT7D5gQZuDDssqYgeFsgrw@mail.gmail.com> <F24437A8-23F9-4DAC-86F6-84A4F1045F5C@gmail.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