1

I am trying to construct a SQL statement that performs the following:

For a specific row of a specific table, for all columns where a value = '{}" , make the value NULL

For example:

table XYZ:

"col_id"    "col2"  "col3"  "col4"  "col5"

1234        {}       {}     PDQ      ABC
5678        {DO}     {RE}   DEF      HIJ
5678        {MI}     {}   ABC      PDQ

If I want: for table XYZ, where col_id = 1234, make all columns with value {} null,

the result would be:

"col_id"    "col2"  "col3"  "col4"  "col5"

1234        NULL     NULL     PDQ      ABC
5678        {DO}     {RE}   DEF      HIJ
5678        {MI}     {}   ABC      PDQ

Grateful for any assistance. Thank you.

4
  • 1
    set col2 = case col2 when '{}' then null else col2 end, col3 = case col3 when '{}' then null else col3 end,... Commented Jul 21, 2022 at 16:54
  • @BryanDellinger thanks, but is there a way to do it without calling out each column explicitly? I have a lot of columns and the table may evolve. I was hoping to keep it more genertic and flexible. Commented Jul 21, 2022 at 17:10
  • @BryanDellinger Or can you add something to show me how to constraint to a specific row at least? Thanks again! Commented Jul 21, 2022 at 17:23
  • @BryanDellinger ok this worked. I figured out your shorthand,,, thanks again Commented Jul 21, 2022 at 18:12

2 Answers 2

1

Use NULLIF:

UPDATE XYZ set col2 = NULLIF(col2, '{}') ... where customer_id = 1234`

Any existing value that is = '{}' will be converted to NULL. UPDATE needs the columns that are updated to be specified so you cannot get out of naming them.

Sign up to request clarification or add additional context in comments.

4 Comments

Adrian, I am not really following part of your answer. where you have value, do you mean column name??
Yes, I will change the answer to reflect.
thanks! works great. A little simpler than Bryans answer (I appreciate both!)
Mostly it was about the fact that NULLIF exists and is a handy function.
0

For a variable set of columns you should use dynamic query by a function like

create or replace function update_columns(id int) returns varchar as
$$
DECLARE
    column_name text;
    set_clause text;
    r_count int;    
    query text;
BEGIN
    FOR column_name IN select i.column_name  FROM information_schema.columns i 
    WHERE table_schema = 'public' and i.column_name !='id' AND table_name   = 'xyz' LOOP
        set_clause:= concat(set_clause, column_name||'=NULLIF('||column_name||', ''{}''), ');
    END LOOP;
    set_clause:=left(set_clause,-2);
    query:= 'UPDATE xyz set '|| set_clause||' where id='||id::varchar;
    RAISE NOTICE '%', query;        
    execute query;
    GET DIAGNOSTICS r_count = ROW_COUNT;    
    return 'Rows affected ='||r_count;
END;
$$ language plpgsql;

Then execute

select update_columns(1234);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.