I have SQL table that has a large number of columns. For some reason, some columns have empty cells instead of NULL cells. I would like to make all empty cells in all the columns to be NULL.
I know that the way to go for a single column is:
UPDATE your_table SET column = NULL WHERE column = ''
However, I am not sure how to execute a similar logic efficiently for all columns without having to write the column names one by one.
Thanks,
I actually use Robert N's answer above daily when I'm importing flat file data sets, so I put it into a stored procedure that I could pass a table name to. It just populates a temp table with the update statements, then executes each row in the table.
Run the following query:
The output of this query will be a chunk of SQL script like this:
Copy and paste that SQL script into a new query and run it to update all your columns.
You could do a query on
syscolumns
to get a list of columns, and use the results to construct your query.Additionally, if you write your query as
then you can do it in a single query without a where clause