+ -
当前位置:首页 → 问答吧 → SQL server 中有什么函数可以为非空的值设置默认值?

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

时间:2011-11-21

来源:互联网

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

作者: rayofdawn   发布时间: 2011-11-21

非空了就是非空的值,不太明白还要什么默认值.

作者: xuam   发布时间: 2011-11-21

你直接设置默认值 这样就不会为空了嘛。

作者: fredrickhu   发布时间: 2011-11-21

创建表的时候设置默认值就不会出现NULL,判断的时候用ISNULL

作者: CainLai   发布时间: 2011-11-21

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

作者: pengxuan   发布时间: 2011-11-21

如果说表字段中没有数据进入的时候添加默认值的话,用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
*/

作者: maco_wang   发布时间: 2011-11-21

热门下载

更多