SqlServer更改字段类型--varchar转decimal

varchar的NULL 无法转为为decimalde NULL,需要先转换为0,再转换为NULL

 

 

在表中的字段类型可以为NULL,且数据全为数字或者“”的情况下,直接把字段类型为varchar更改为decimal有可能会提示“将数据类型 varchar 转换为 decimal 时出错”,可以先把字段类型更改为int,然后在把字段类型更改为decimal,但是这样会把此字段值为“”的数据全部改为0,如果数据为0有影响的话,可以再把值为0的数据改为NULL。

原表设计:

  

 

原数据:

  

 

  alter table temp alter column phone int  null

  alter table temp  alter column phone  decimal  null

更改后的表设计:

  

 

更改后的表数据:

  

 update temp set phone=NULL where phone=0

  

posted @ 2022-03-23 15:05  KJXY  阅读(2396)  评论(0编辑  收藏  举报