How do I change all empty strings to NULL in a table?

前端 未结 7 1431
悲哀的现实
悲哀的现实 2020-12-08 20:25

I have a legacy table with about 100 columns (90% nullable). In those 90 columns I want to remove all empty strings and set them to null. I know I can:

updat         


        
相关标签:
7条回答
  • 2020-12-08 21:03

    There isn't a standard way - but you can interrogate the system catalog to get the relevant column names for the relevant table and generate the SQL to do it. You can also probably use a CASE expression to handle all the columns in a single pass - a bigger SQL statement.

    UPDATE Table
       SET Column1 = CASE Column1 = ' ' THEN NULL ELSE Column1 END,
           ...
    

    Note that once you've generated the big UPDATE statement, all the work is done down in the server. This is much more efficient than selecting data to the client application, changing it there, and writing the result back to the database.

    0 讨论(0)
提交回复
热议问题