Thursday, November 7, 2013

Update All Column Values of a table in SQL

You can very easily generate SQL to do the updates, to save you a lot of typing. You can whip up something like this very quickly:

SELECT 'UPDATE [Name] SET ' + COLUMN_NAME + ' = NULL WHERE + ' + COLUMN_NAME + ' = '' ''' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Name' AND IS_NULLABLE = 'YES' AND DATA_TYPE IN ('char', 'nchar', 'varchar', 'nvarchar')

Just replace "Name" with the name of your table, and run. Copy the output into a SQL Server Query Manager window, and run it.
The above will update every column of every row in the table to be NULL where the column contains exactly one space.

No comments:

Post a Comment