怎样把一条SQL语句查询出的结果集中的空值替换成0

来源:百度知道 编辑:UC知道 时间:2024/05/24 17:23:38
关键是我不知道是哪个为空,而且有很多字段,怎么办?

ISNULL(字段,0)

在Sql2000中
假设有这么一张表
create table test
(
id int,
name varchar(32),
sex char
)
那么查询可以用
select * from test where id is null or name is null or sex is null
有些东西是不能为空的,所以LZ有很多字段的话可以挑选出那些可能为空的,其余在建立表时就应当设置为非空拉

如果在Oracle中有个 nvl(exp1,exp2)函数,
可以用来判断是否为空,如果exp1为空的话,则显示exp2中的字符
select nvl(id,'空'),nvl(name,'空'),nvl(sex,'空') from name

望能看到更好的办法.

select decode(D1,null,'0',D1) from test;

如果你是SQL 数据库:
有一表Table,字段:ID,name(字符型),如果字段name为空NULL或为空字符‘’的替换为‘0’,用以下语句:
select (CASE when (name IS NULL or name='') then '0' else name end) as name from table
如果name为数据型的,则:
select (CASE when (name IS NULL or name='') then 0 else name end) as name from table
你试试看,我已试过的。

select case ziduan when is null then '' end

select nvl(xxx,0) from table
xxx为字符型时加上单引号