Home > Net >  postgresql, concat json values if not null
postgresql, concat json values if not null

Time:05-12

I'm using CONCAT with values from a jsonb columns. sometimes one of those values could be null (json).

how can i COALESCE the whole returned field xywh to NULL ?

query := `SELECT
              CONCAT( 
                data->'x',',',
                data->'y',',', 
                data->'w',',', 
                data->'h') as xywh 
              FROM table`

CodePudding user response:

Because concat fucntion will ignore NULL

Concatenate all arguments. NULL arguments are ignored.

if you want to let the result be NULL if any JSON field is null, we can try to use || concatenate string.

select  data->>'x' || ',' ||
        data->>'y' || ',' ||
        data->>'w' || ',' ||
        data->>'h'  
FROM table`
  • Related