Often when importing data in SQL, you end up with cells that contain empty strings, spaces only or the like. In order to keep a clean database, I like to convert these to NULL values. In case you would like to do this as well, I’ve posted my script that iterates all columns in a temp table and converts empty cells to NULL values.
-- Declare the cursor as read only and select the specified temp table's field names.
Declare objESNCur CURSOR
FOR Select Column_Name
Where Object_ID('tempdb..#TableToClean') = Object_ID('tempdb..'+TABLE_NAME)
-- Declare a variable to contain the field name for the current iteration.
Declare @Column_Name nvarchar(255)
-- Open the cursor
-- Fetch the first field name into the variable.
-- Loop while there are no errors and there are still rows to be fetched.
While (@@fetch_status <> -1)
-- Loop while there are still rows to be fetched.
If (@@fetch_status <> -2)
-- Make sure field name isn't a field I don't want to cleanup.
If (@Column_Name != 'NotThisField')
-- Declare a temporary variable to house a dynamic SQL statement.
Declare @ESNSQL nvarchar(4000)
-- Set the SQL variable to update blank fields to NULLs.
Set @ESNSQL = 'Update #TableToClean
Set ' + @Column_Name + ' = Null
Where LTrim(RTrim(' + @Column_Name + ')) = '''''
-- Execute the dynamic SQL statement.
Exec sp_executesql @ESNSQL
-- Fetch the next row into the field name variable.
-- Close the cursor.
-- Deallocate the cursor resources.
If this has been of help to you, please feel free to leave me a comment.