这是什么意思?其中 isnull(列,) <=



有人能帮我理解吗:

where isnull(column,' ') <= ' '

在SQL中,这是where子句,它看起来像是在比较小于或等于空格的空格?这是什么意思?有人能发光吗。

所以我承认我看不出这句话有任何好处或目的,但测试和查看结果非常容易。

您可能会期望,一眼望去,它会选择列为null的行,或者按字母顺序进行比较时,选择";较少的";而不是一个空格,它基本上都是不可打印的ascii字符。

它甚至没有做到这一点,因为小于32(ascii空间)的字符代码的比较不起作用(不足为奇)。

有一点测试数据

create table t (ch nvarchar(2), id int)
insert into t select 'A'              ,1
insert into t select ' '              ,2
insert into t select '3'              ,3
insert into t select ''               ,4
insert into t select null             ,5
insert into t select Char(13)         ,6
insert into t select Char(15)         ,7
insert into t select Char(31)+Char(30),8

您可能期望行CCD_;小于或等于";一个空间。

select * 
from t
where IsNull(ch,' ') <= ' '

然而,这会返回2,4,5,因此空字符串被认为小于空格,但小于32的ascii值则不然。

如果你真的想找到所有小于或等于空格(32)的ascii值,你必须使用ascii函数,它可以准确地给出预期的结果

select *
from t
where IsNull(ascii(ch),32)<=32

它返回CCD_ 6-它排除了明显为"0"的A和3;较高的";但包括空白值,因为其评估为null,因此为32。

至于它实际完成了什么——只有适当的样本数据和完整的上下文才能回答。

相关内容

最新更新