当前位置: 代码迷 >> Sql Server >> SQL server 中有什么函数可认为非空的值设置默认值
  详细解决方案

SQL server 中有什么函数可认为非空的值设置默认值

热度:85   发布时间:2016-04-27 11:55:56.0
SQL server 中有什么函数可以为非空的值设置默认值?
coalesce可以为为空的列设置默认 coalesce(name,' ')
有什么函数是反过来,可以在非空的时候设置默认值?


------解决方案--------------------
SQL code
if object_id('tb','U') is not null   drop table tbgocreate table tb( id int identity(1,1), name varchar(10), age int not null default 10  --非空,默认值是10)go
------解决方案--------------------
如果说表字段中没有数据进入的时候添加默认值的话,用default就可以了。
但是字段有了default是不会出现null的情况的。


SQL code
declare @t table (col varchar(1))insert into @tselect 'a' union allselect 'b' union allselect null union allselect 'c' union allselect 'd' union allselect nullselect col=isnull(replace(col,col,'1'),'0') from @t/*col------110110*/
  相关解决方案