Replace empty cells with NULL values in large number of columns

后端 未结 3 843
挽巷
挽巷 2021-02-01 09:57

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 column

3条回答
  •  误落风尘
    2021-02-01 10:25

    You could do a query on syscolumns to get a list of columns, and use the results to construct your query.

    select quotename(name) + ' = nullif (' + quotename(name)+ ','''')'
    from syscolumns 
    where id = object_id('yourtable')
    

    Additionally, if you write your query as

    update yourtable
    set
        yourcolumn=nullif(yourcolumn, ''),
        yourcolumn2=nullif(yourcolumn2, ''),
        ...    
    

    then you can do it in a single query without a where clause

提交回复
热议问题