Friday, May 2, 2014

T-sql Query to update all NULL values with Empty String in all columns of a table

select 'update ' + table_name + ' set [' + column_name + '] = '  +
 CASE
    WHEN DATA_TYPE = 'bit' THEN '0'
    WHEN DATA_TYPE = 'int' THEN '0'
    WHEN DATA_TYPE = 'decimal' THEN '0'
    WHEN DATA_TYPE = 'date' THEN '''1/1/1900'''
    WHEN DATA_TYPE = 'datetime' THEN '''1/1/1900'''
    WHEN DATA_TYPE = 'uniqueidentifier' THEN '00000000-0000-0000-0000-000000000000'
    ELSE '''''' -- everything else get's an empty string
  END
+

' where [' + column_name + '] is null'
from information_schema.columns
where table_name = 'YourTable'

No comments: