首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

取消空值?该怎么解决

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

取消空值?
我在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 pubsGOSELECT AVG(ISNULL(price, $10.00))FROM titlesGO下面是结果集:-------------------------- 14.24                      (1 row(s) affected)
[解决办法]
SQL code
use testgoif object_id('test.dbo.tb')is not null drop table tbgocreate table tb(id int identity(1,1),col int)goinsert into tb select 19union all select nullunion all select 5union all select nullunion all select 45goselect * from tb/*id col------------1    192    NULL3    54    NULL5    45*/select id,col=isnull(rtrim(col),'') from tb/*id col------------1    192    3    54    5    45*/select * from tb where col is not null/*id col------------1    193    55    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

热点排行