谁了解isnull函数,解释一下

shenen 2010-10-12 01:29:06
语句如下:
select isnull(b.checked,''),b.checked
from task
left join (select id,'checkded' as checked from task where id in (1,2,3)) b on task.id=b.id
结果如下:
checkded checkded
checkded checkded
checkded checkded
checkded NULL
checkded NULL
checkded NULL

不明白为什么会出现这样的结果。
...全文
352 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
shenen 2010-10-12
  • 打赏
  • 举报
回复
Microsoft SQL Server 2000 - 8.00.2039 (Intel X86) May 3 2005 23:18:38 Copyright (c) 1988-2003 Microsoft Corporation Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
-------------------------------
不用试了,打完补丁就正常了,呵呵!
以前一直打着补丁的,这次没注意,结果就出问题了。
SQLCenter 2010-10-12
  • 打赏
  • 举报
回复
这样呢?

select top 5 id = identity(int,1,1) into # from syscolumns

select *, 'checkded' as checked into #1 from # where id<=3

select isnull(b.checked,''),b.checked
from # a
left join
#1 b on a.id=b.id

drop table #,#1
shenen 2010-10-12
  • 打赏
  • 举报
回复
我现在打sp4补丁,打完再试了。
shenen 2010-10-12
  • 打赏
  • 举报
回复
select top 5 id = identity(int,1,1) into # from syscolumns

select isnull(b.checked,''),b.checked
from # task
left join
(select id,'checkded' as checked from # where id in (1,2,3)) b on task.id=b.id

drop table #
-----------------------
结果相同
SQLCenter 2010-10-12
  • 打赏
  • 举报
回复
#/task 加上别名,用别名访问,再测试一下
shenen 2010-10-12
  • 打赏
  • 举报
回复
我的已经说了,上面有。
SQLCenter 2010-10-12
  • 打赏
  • 举报
回复
select @@version

/*
Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (Intel X86)
Mar 29 2009 10:27:29
Copyright (c) 1988-2008 Microsoft Corporation
Enterprise Edition on Windows NT 6.1 <X86> (Build 7600: )
*/


也说一下你的版本吧,这么怪异的BUG
shenen 2010-10-12
  • 打赏
  • 举报
回复
你说一下你的sqlserver版本号。
shenen 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 sqlcenter 的回复:]
如果说这样的语句能出来你这样的结果,那你搞点测试数据过来。

这是我的测试,除了tase替换成#其它完全一样:


SQL code
select top 5 id = identity(int,1,1) into # from syscolumns

select isnull(b.checked,''),b.checked
from #
left join
(select……
[/Quote]
我执行了你的语句:
checkded checkded
checkded checkded
checkded checkded
checkded NULL
checkded NULL
SQLCenter 2010-10-12
  • 打赏
  • 举报
回复
如果说这样的语句能出来你这样的结果,那你搞点测试数据过来。

这是我的测试,除了tase替换成#其它完全一样:

select top 5 id = identity(int,1,1) into # from syscolumns

select isnull(b.checked,''),b.checked
from #
left join
(select id,'checkded' as checked from # where id in (1,2,3)) b on #.id=b.id

drop table #

/*
checked
-------- --------
checkded checkded
checkded checkded
checkded checkded
NULL
NULL

(5 行受影响)

*/
shenen 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 billpu 的回复:]
select isnull(b.checked,''),b.checked
from task
left join (select id,'checkded' as checked from task where id in (1,2,3)) b on task.id=b.id
注意看红色部分 你的字段内容全是字符串"checked"没有空值
[/Quote]

这个解释还说得过去,但是我没有查到微软提供的权威资料,以前也经常用sqlserver啊,也没注意会出现这样的问题。
我这次装好sqlserver后也没设置什么。
shenen 2010-10-12
  • 打赏
  • 举报
回复
就是不太好理解。查询和结果都是真实的,我没必要做个假数据拿大伙开玩笑。
shenen 2010-10-12
  • 打赏
  • 举报
回复
select isnull(b.checked,''),b.checked+'',b.checked,big_type,big_type+''
from task
left join (select id,'checkded' as checked from task where id in (1,2,3)) b on task.id=b.id

--结果
1 checkded checkded checkded NULL NULL
2 checkded checkded checkded 行政事务 行政事务
3 checkded checkded checkded 行政事务 行政事务
4 checkded checkded NULL 办公电话 办公电话
5 checkded checkded NULL 邮件 邮件
6 checkded checkded NULL 综合系统 综合系统
7 checkded checkded NULL 综合系统 综合系统
baiying15 2010-10-12
  • 打赏
  • 举报
回复
结果真如上面说的?isnull出来的那个值我觉得正常,但是后面那个NULL是咋个出来的哦?我朗格就看不懂啦。
shenen 2010-10-12
  • 打赏
  • 举报
回复
select @@version

Microsoft SQL Server 2000 - 8.00.194 (Intel X86) Aug 6 2000 00:57:48 Copyright (c) 1988-2000 Microsoft Corporation Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
SQLCenter 2010-10-12
  • 打赏
  • 举报
回复
重新看一看,你的结果是不可能出现的,结果应该是这个

checkded checkded
checkded checkded
checkded checkded
NULL
NULL
NULL


除非是
select isnull(a.checked,''),b.checked
shenen 2010-10-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 zsh0809 的回复:]
首先分清,什么是空值,什么是null值?
isnull的语法参考msdn文档。
举个列子:


SQL code
declare @str varchar(20)

set @str=null
select isnull(@str,'new value') -----返回结果为'new value'

set @str=''
select isnull(@str,'new……
[/Quote]


这个容易理解,就是上面那个不好理解,本来右边的记录也显示为null,但是用isnull函数时,他又认为不是NULL值
「已注销」 2010-10-12
  • 打赏
  • 举报
回复
首先分清,什么是空值,什么是null值?
isnull的语法参考msdn文档。
举个列子:

declare @str varchar(20)

set @str=null
select isnull(@str,'new value') -----返回结果为'new value'

set @str=''
select isnull(@str,'new value') -----返回结果为''

set @str='abc'
select isnull(@str,'new value') -----返回结果为'abc'
billpu 2010-10-12
  • 打赏
  • 举报
回复
select isnull(b.checked,''),b.checked
from task
left join (select id,'checkded' as checked from task where id in (1,2,3)) b on task.id=b.id
注意看红色部分 你的字段内容全是字符串"checked"没有空值
shenen 2010-10-12
  • 打赏
  • 举报
回复
>>isnull 把控制替换为后面的那个参数
>>你的语句select出的就是'checked'字符串和isnull没关系
isnull是遇到空值转成第二个参数啊,不知他为什么不转换啊
加载更多回复(3)

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧