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

取消空值?
我在select语句中加入了case函数,得到的结果中有null,我想取消空值,只返回有数据的值,还要加个什么函数?

------解决方案--------------------
SQL code
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。

示例
A. 将 ISNULL 与 AVG 一起使用
下面的示例查找所有书的平均价格,用值 $10.00 替换 titles 表的 price 列中的所有 NULL 条目。

USE pubs
GO
SELECT AVG(ISNULL(price, $10.00))
FROM titles
GO

下面是结果集:

-------------------------- 
14.24                      

(1 row(s) affected)

------解决方案--------------------
SQL code
use test
go

if object_id('test.dbo.tb')is not null drop table tb
go
create table tb
(
id int identity(1,1),
col int
)
go
insert into tb select 19
union all select null
union all select 5
union all select null
union all select 45
go

select * from tb
/*
id col
------------
1    19
2    NULL
3    5
4    NULL
5    45
*/

select id,col=isnull(rtrim(col),'') from tb

/*
id col
------------
1    19
2    
3    5
4    
5    45
*/

select * from tb where col is not null

/*
id col
------------
1    19
3    5
5    45
*/

------解决方案--------------------
探讨
select case id when 1 then name end from test
/*
张三
null
null
*/
我想只得到结果
/*
张三
*/

不用下面的语句哈
select * from test where id=1


------解决方案--------------------
SQL code
select MAX(case id when 1 then name   end ) AS name 
from test

------解决方案--------------------
探讨
SQL code
select MAX(case id when 1 then name end ) AS name
from test