现在一张表,里面的一个字段有值,有的没有值,但显示的时候不能显示为空,例子:
Name----------Sex---------Age
张三----------男---------15
李四---------男---------Null
王五---------男---------19
赵六---------男---------Null查询出来的效果是
Name----------Sex---------Age
张三----------男---------15
李四---------男---------0
王五---------男---------19
赵六---------男---------0
其中李四和赵六的年龄不知,就显示为0
这个强大的SQl怎么写呀???
Name----------Sex---------Age
张三----------男---------15
李四---------男---------Null
王五---------男---------19
赵六---------男---------Null查询出来的效果是
Name----------Sex---------Age
张三----------男---------15
李四---------男---------0
王五---------男---------19
赵六---------男---------0
其中李四和赵六的年龄不知,就显示为0
这个强大的SQl怎么写呀???
sex,
isnull(age,0)age
from tb
(
[name] varchar(10),
age int
)
insert into #tb values('a',10);
insert into #tb(name) values('a');select name,isnull(age,0) from #tb
if object_id('tb') is not null drop table tb
gocreate table tb([Name] char(10), Sex char(4), Age int)
insert into tb select '张三', '男', 15
union all select '李四', '男', null
union all select '王五', '男', 19
union all select '赵六', '男', nullselect [Name], Sex, isnull(Age, 0) as Age from tb
结果
张三 男 15
李四 男 0
王五 男 19
赵六 男 0
用:isnull 函数就可以搞定的
select [name],sex,isnull(Age,0) from tb
ISNULL
使用指定的替换值替换 NULL。语法
ISNULL ( check_expression , replacement_value ) 参数
check_expression将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。replacement_value在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。 返回类型
返回与 check_expression 相同的类型。注释
如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。
sex,
isnull(age,0)age
from
支持