读书人

怎样在查询中判断某列为空值?该怎么处

发布时间: 2012-01-07 21:41:56 作者: rapoo

怎样在查询中判断某列为空值?
如题。请教is null的具体用法。
可否举个例子

[解决办法]
select *
from 表名
where 字段名 = null

[解决办法]
--不能用=null来判断字段值是否为空,可以看一下例子:


create table #t (f1 varchar(100),f2 varchar(100))
insert into #t select 'aaa ', '111 '
insert into #t select 'bbb ',null

--这是对的
select *
from #t
where f2 is null


--这样的查询结果是不对的
select *
from #t
where f2 = null


drop table #t

[解决办法]
判断某列的值是否为null,使用is null,例子见1楼

另外,isnull()函数是用来将null转换为需要的值,例如,select isnull(colname, '123 ') from table1

[解决办法]
where 字段名 is null
[解决办法]
“=”是用在实际的对象分量上的,而“IS”是用在逻辑意义上的

读书人网 >SQL Server

热点推荐