Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update JSONB column with NULL value in PostgreSQL

I'm trying to update the following y.data, which is a JSONB type column that currently contains a NULL value. The || command does not seem to work merging together y.data with x.data when x.data is NULL. It works fine when x.data contains a JSONB value.

Here is an example query.

UPDATE x
SET x.data = y.data::jsonb || x.data::jsonb
FROM (VALUES ('2018-05-24', 'Nicholas', '{"test": "abc"}')) AS y (post_date, name, data)
WHERE x.post_date::date = y.post_date::date AND x.name = y.name;

What would be the best way to modify this query to support updating x.data for rows that both have existing values or are NULL?

like image 388
Nicholas Tulach Avatar asked Apr 18 '26 20:04

Nicholas Tulach


1 Answers

Concatenating anything with null produces null. You can use coalesce() or conditional logic to work around this:

SET x.data = COALESCE(y.data::jsonb || x.data, y.data::jsonb)

Or:

SET x.data = CASE WHEN x.data IS NULL 
    THEN y.data::jsonb
    ELSE y.data::jsonb || x.data
END

Note that there is no need to explictly cast x.data to jsonb, since it's a jsonb column already.

like image 95
GMB Avatar answered Apr 20 '26 09:04

GMB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!