Re: Concatenating NULL with JSONB value return NULL

From: Melvin Davidson <melvin6925(at)gmail(dot)com>
To: John R Pierce <pierce(at)hogranch(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Concatenating NULL with JSONB value return NULL
Date: 2016-12-18 23:49:40
Message-ID: CANu8FiwcGP9NMtGqoTszT+r6hgXS0J1C7zLmvREO_ie7ExEGWw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Dec 18, 2016 at 6:08 PM, John R Pierce <pierce(at)hogranch(dot)com> wrote:

> On 12/18/2016 2:52 PM, Jong-won Choi wrote:
>
>>
>> I have a NULL-able JSONB type column and want to perform upsert,
>> concatenating with the existing value.
>>
>
> NULL does not mean 'NO' value in SQL it means UNKNOWN value. sort of like
> the 'indeterminate' in math.
>
> maybe you want a NOT NULL json value that you set to '' or something when
> its empty.
>
>
> --
> john r pierce, recycling bits in santa cruz
>
>
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

Have you tried using CASE?

INSERT INTO Fan (oid, campaigns, facts) VALUES (189,'{"campaign-id":
"12345"}','{"attended": false}')
ON CONFLICT (oid)
DO UPDATE SET campaigns = EXCLUDED.campaigns,
CASE WHEN fan.facts is NULL
THEN facts = EXCLUDED.facts
ELSE facts = fan.facts || EXCLUDED.facts
END
RETURNING *;

--
*Melvin Davidson*
I reserve the right to fantasize. Whether or not you
wish to share my fantasy is entirely up to you.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gavin Flower 2016-12-19 00:23:09 Re: About the MONEY type
Previous Message John R Pierce 2016-12-18 23:08:24 Re: Concatenating NULL with JSONB value return NULL