日期:2014-05-18  浏览次数:20479 次

SQL server 中有什么函数可以为非空的值设置默认值?
coalesce可以为为空的列设置默认 coalesce(name,' ')
有什么函数是反过来,可以在非空的时候设置默认值?


------解决方案--------------------
SQL code

if object_id('tb','U') is not null
   drop table tb
go
create 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 @t
select 'a' union all
select 'b' union all
select null union all
select 'c' union all
select 'd' union all
select null

select col=isnull(replace(col,col,'1'),'0') from @t
/*
col
------
1
1
0
1
1
0
*/