Why
为什么
select 1 - NULL
returns NULL instead of 1?
返回NULL而不是1?
It wasn't clearly expected to me.
对我来说,这并不是我想要的。
3 个解决方案
#1
1
Null: A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown.
Null: Null值表示该值是未知的。空值与空值或零值不同。没有两个空值是相等的。两个空值之间或null值与任何其他值之间的比较返回未知,因为每个空值的值都是未知的。
If you do any arithmetic operations with null the whole expression evaluates to Null. In order to handle null you should use Isnull() or coalesce function like this.
如果对null进行算术运算,则整个表达式的值为null。为了处理null,您应该使用Isnull()或这样的合并函数。
select 1 - isnull(NULL,0) as result
#2
3
Simply because NULL
is not 0
.
因为NULL不是0。
If it helps, consider NULL
as a synonym for "unknown", and then it'll make perfect sense - the result of 1 minus an unknown number can only give an unknown result.
如果有帮助的话,可以将NULL看作“unknown”的同义词,然后它就会非常有意义——1减去一个未知数的结果只能得到一个未知的结果。
#3
0
Here are some references on how NULL
behaves differently:
这里有一些关于NULL的不同行为的参考:
-
NULL
can be thought of as UNKNOWN (docs). - NULL可以被认为是未知的(文档)。
- Arithmetic operations with
NULL
result inNULL
(wiki). - 使用NULL的算术操作导致NULL (wiki)。
-
SUM()
operation ignoresNULL
instead of returning UNKNOWN (docs). - SUM()操作忽略NULL而不返回未知(docs)。
- String concatenation with
NULL
result inNULL
(wiki and comment by @Zohar). - 字符串连接与空结果为空(wiki和@Zohar的注释)。
- Boolean comparisons with
NULL
use three-value logic (wiki). - 使用三值逻辑(wiki)将布尔值与空值进行比较。
- Where clauses with
NULL
should useIS
, because boolean comparisons result in UNKNOWN notTRUE
(docs). - 使用NULL的子句应该是IS,因为布尔比较会导致未知不是TRUE (docs)。
To determine whether an expression is NULL, use IS NULL or IS NOT NULL instead of comparison operators (such as = or !=). Comparison operators return UNKNOWN when either or both arguments are NULL.
要确定表达式是否为NULL,使用为NULL或不是NULL而不是比较运算符(例如=或!=)。当其中一个或两个参数都为空时,比较运算符返回未知值。
#1
1
Null: A value of NULL indicates that the value is unknown. A value of NULL is different from an empty or zero value. No two null values are equal. Comparisons between two null values, or between a NULL and any other value, return unknown because the value of each NULL is unknown.
Null: Null值表示该值是未知的。空值与空值或零值不同。没有两个空值是相等的。两个空值之间或null值与任何其他值之间的比较返回未知,因为每个空值的值都是未知的。
If you do any arithmetic operations with null the whole expression evaluates to Null. In order to handle null you should use Isnull() or coalesce function like this.
如果对null进行算术运算,则整个表达式的值为null。为了处理null,您应该使用Isnull()或这样的合并函数。
select 1 - isnull(NULL,0) as result
#2
3
Simply because NULL
is not 0
.
因为NULL不是0。
If it helps, consider NULL
as a synonym for "unknown", and then it'll make perfect sense - the result of 1 minus an unknown number can only give an unknown result.
如果有帮助的话,可以将NULL看作“unknown”的同义词,然后它就会非常有意义——1减去一个未知数的结果只能得到一个未知的结果。
#3
0
Here are some references on how NULL
behaves differently:
这里有一些关于NULL的不同行为的参考:
-
NULL
can be thought of as UNKNOWN (docs). - NULL可以被认为是未知的(文档)。
- Arithmetic operations with
NULL
result inNULL
(wiki). - 使用NULL的算术操作导致NULL (wiki)。
-
SUM()
operation ignoresNULL
instead of returning UNKNOWN (docs). - SUM()操作忽略NULL而不返回未知(docs)。
- String concatenation with
NULL
result inNULL
(wiki and comment by @Zohar). - 字符串连接与空结果为空(wiki和@Zohar的注释)。
- Boolean comparisons with
NULL
use three-value logic (wiki). - 使用三值逻辑(wiki)将布尔值与空值进行比较。
- Where clauses with
NULL
should useIS
, because boolean comparisons result in UNKNOWN notTRUE
(docs). - 使用NULL的子句应该是IS,因为布尔比较会导致未知不是TRUE (docs)。
To determine whether an expression is NULL, use IS NULL or IS NOT NULL instead of comparison operators (such as = or !=). Comparison operators return UNKNOWN when either or both arguments are NULL.
要确定表达式是否为NULL,使用为NULL或不是NULL而不是比较运算符(例如=或!=)。当其中一个或两个参数都为空时,比较运算符返回未知值。