Replacing all 0s with Nulls in a table

69 Views Asked by At

Is there a way to replace all 0s with a null for all the columns of a table ? I know how to do it for single column, I am searching for something like dynamic query for all the columns.

1

There are 1 best solutions below

7
Gordon Linoff On

This answers the original version of the question.

You can write one update query, but you need to list all columns:

update t
    set col1 = nullif(col1, 0),
        col2 = nullif(col2, 0),
        . . . ;

You might want to add:

where col1 = 0 or col2 = 0 or . . .

To limit the number of rows that are processed.