Home > Back-end >  How to use concat_ws() to return multiple values into different keys: PostgreSQL
How to use concat_ws() to return multiple values into different keys: PostgreSQL

Time:08-04

Related to this Is there a way to return all non-null values even if one is null in PostgreSQL? - the solution of which allowed me to return null values, however it returns it into the same key instead of the one assigned.

For this example in particular so I'd like to insert it as A=valueOfA, B=valueOfB instead of A=valueOfA,valueOfB.

    select concat_ws(",", A, B, C) into D;
    // if C is null, it will return A=valueOfA,valueOfB

Thanks! :)

CodePudding user response:

It might be easier to simply generate a JSON value:

jsonb_build_object('a', a, 'b', b, 'c', c)

This would e.g. include "a": null if column a was null. If you want to remove those, use jsonb_strip_nulls:

jsonb_strip_nulls(jsonb_build_object('a', a, 'b', b, 'c', c))
  • Related