Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lK5HF-00019Q-H1 for pgsql-general@arkaria.postgresql.org; Wed, 10 Mar 2021 20:17:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lK5HE-0007t4-Ev for pgsql-general@arkaria.postgresql.org; Wed, 10 Mar 2021 20:17:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lK5HE-0007sx-7k for pgsql-general@lists.postgresql.org; Wed, 10 Mar 2021 20:17:52 +0000 Received: from mail-ej1-x632.google.com ([2a00:1450:4864:20::632]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lK5HB-0000Mq-0X for pgsql-general@postgresql.org; Wed, 10 Mar 2021 20:17:51 +0000 Received: by mail-ej1-x632.google.com with SMTP id p8so41296104ejb.10 for ; Wed, 10 Mar 2021 12:17:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=N1rJ8pRP3UF2FYAsvhoRHP9NsClgW5lnvzPLck6JfY4=; b=lnnjkH8RYzQxxocBnFhG/nR0siWTl5DWI0ui1bW4y403zUdLrtprq9rE4ffxzgpJcn NNDHFvikcZTJc7eJT0s3Ziaok9+3YGSj0qN6U13vajQVBOQ0/qzPOXYoiXnDCPw+4Ewo fUEU9rVQjR9d8cUvXa7052SzDjMcnW7HDfE/evHcLHdXLvSdRITZf2P7uCnDupwHHnMF 9gyUdW9b6P2Pb6I7/h39NeKG5OfPzEj2rky3uC2EHdE137UDygZlLRLBKom2Q2E1ftqk jSaODUiOosKYrZuUNRRp3d+ao1eDrwhRJjtGC080IODeeOW6+w/quwWEZyh3VVItL2Mu /4IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=N1rJ8pRP3UF2FYAsvhoRHP9NsClgW5lnvzPLck6JfY4=; b=ZBM++l3FYj+ksIgDfRDPUHkyZJIVsFzn6fzvA4nC2Yt3G3fGqO29RSlZg1BIX/bsu+ my1e+bX9q2UBJhqY9u9slLKF64mihhwKcvYrnDKPGmfeqC5mdDUWOpL33H0lcTrXzle9 7wXor+UBS5mO35/Hj34VLF9j541cEFkn3en8yl3dziEvao1RH9hGtMf4zdEVqkmJTSgw xu8MDgxp6FhlJkUijQYo6AHSRfw25Rd64zCZ71E+lxV1ZB399aMJ9O4eJd9UIHnCnDxq yN1qwp19zdTXRylyLOCnc3tbs6ZshspKHPEMXIHirmYrvQsMa7caUYWRt4A9XV1nYVwC MJ7Q== X-Gm-Message-State: AOAM532vCgkPCf+wb33tXqXVoMttFAyvPlXq7giiWY1XdZMnuZn60caW 75ONP2mrkJYseld/KIXzXOE= X-Google-Smtp-Source: ABdhPJzmVWNYDQX8YGA2dkWgb09VHapR18VxZGri7br3u/ifVNprEOMhCL7FpJJQ4sJIE9tcHbHKKQ== X-Received: by 2002:a17:906:d71:: with SMTP id s17mr214543ejh.126.1615407468182; Wed, 10 Mar 2021 12:17:48 -0800 (PST) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id pg2sm254151ejb.49.2021.03.10.12.17.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Mar 2021 12:17:47 -0800 (PST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.17\)) Subject: Re: How to get CASE statement to recognize null ? From: Alban Hertroys In-Reply-To: Date: Wed, 10 Mar 2021 21:17:46 +0100 Cc: Tom Lane , Postgres General Content-Transfer-Encoding: quoted-printable Message-Id: References: <2188918.1615405613@sss.pgh.pa.us> To: David Gauthier X-Mailer: Apple Mail (2.3445.104.17) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 10 Mar 2021, at 21:00, David Gauthier = wrote: >=20 > Hey, Thanks Tom ! >=20 > It's actually a little more complicated than the simple example, so = I'm not sure it can be shoehorned into coalesce... >=20 > CASE=20 > 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 wrote: > David Gauthier writes: > > dvdb=3D# select > > CASE col1 > > WHEN null THEN 'z' > > ELSE col1 > > END as col1, > > col2 > > from foo; >=20 > This test is equivalent to "col1 =3D null" which will always fail. > You could try something like >=20 > CASE WHEN col1 IS NULL THEN ... ELSE ... END >=20 > Although I think the particular thing you're doing here would > be better solved with COALESCE(col1, 'z'). >=20 > 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.