Posts

Showing posts from June, 2016

Quickly Replace Blank Values with NULLs in Entire Table

When importing flat-file data, a lot of processes don't necessarily allow for automatic use of a NULL in place of blank ('') values. I needed a quick way to update every single blank value to a NULL to clean up the data post-import in these situations. I found a very easy code snippet to pull the dynamic update statements necessary, but even this little bit of extra copy-paste-execute work seemed tedious after 10+ times. Code from StackOverflow poster Robert N: (http://stackoverflow.com/a/19057017/3015030) SELECT ('UPDATE '+@tablename+' SET ['+name+'] = NULL WHERE ['+name + '] = '''';') FROM syscolumns WHERE id = OBJECT_ID(@tablename) AND isnullable = 1; My addition to this was to automate the whole thing into a stored procedure. This SPROC accepts a fully qualified table name (or locally qualified name) and creates the statements, then passes to a cursor to execute each. I created it in my [master] database so that I could us