简体   繁体   中英

How to replace all the NULL values in postgresql?

I found the similar question and solution for the SQL server. I want to replace all my null values with zero or empty strings. I can not use the update statement because my table has 255 columns and using the update for all columns will consume lots of time.

Can anyone suggest to me, how to update all the null values from all columns at once in PostgreSQL?

If you want to replace the data on the fly while selecting the rows you need:

SELECT COALESCE(maybe_null_column, 0)

If you want the change to be saved on the table you need to use an UPDATE . If you have a lot of rows you can use a tool like pg-batch

You can also create a new table and then swap the old one and the new one:

# Create new table with updated values
CREATE TABLE new_table AS
SELECT COALESCE(maybe_null_column, 0), COALESCE(maybe_null_column2, "")
FROM my_table;

# Swap table
ALTER TABLE my_table RENAME TO obsolete_table;
ALTER TABLE new_table RENAME TO my_table;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM