需要:1 + null = 1 [英] needed: 1 + null = 1

查看:178
本文介绍了需要:1 + null = 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要这样的行为:1 + null = 1

我有一组(动态)包含我想要的小数的多列

添加如下:

如果所有列都为null,则结果应为null

如果不是所有列都为null,则空列可能为空被视为0.


例如

null + null + 1 = 1

null + null + null = null


问题是第一个表达式产生null。


到目前为止,我用isnull生成了一个更新语句(< column>,0) ,然后
,然后第二个表达式得到0.

我可以添加另一个更新语句,如果所有

列都为null,则将结果设置为null ,但这很慢,而且不是很直观



如果有像''concat null yield
$这样的设置会有多好b $ b null''用于算术运算符。


任何人都知道如何解决这个问题?


谢谢。

Paul

Hi,

I need this behaviour: 1 + null = 1
I have a (dynamic) set of many columns containing decimals that I want
to add as follows:
if all columns are null the result should be null
if not all columns are null, the null columns may be regarded as 0.

E.g.
null + null + 1 = 1
null + null + null = null

The problem is that the first expression yields null.

Up till now I generated an update statement with isnull(<column>,0),
however, then the second expression yields 0.
I can add another update statment setting the result to null if all
columns are null, but this is very slow, and not very intuitive
either.
How nice it would be if there were a setting like ''concat null yields
null'' for arithmetic operators.

Anyone any idea how to fix this?

Thanks.
Paul

推荐答案

in ** @ vanoordt.nl 在新闻中写道:1180430739.196981.227870

@ q69g2000hsb.googlegroups.com:
in**@vanoordt.nl wrote in news:1180430739.196981.227870
@q69g2000hsb.googlegroups.com:




我需要这个行为:1 + null = 1

我有一组(动态)包含我想要的小数的多列br />
添加如下:

如果所有列都为null,则结果应为null

如果不是所有列都为null,则空列可能为被视为0.


例如

null + null + 1 = 1

null + null + null = null


问题是第一个表达式产生null。


到目前为止,我用isnull(< column>,0)生成了一个更新语句,

然而,第二个表达式得到0.

如果所有

列都为null,我可以添加另一个更新语句,将结果设置为null ,但这很慢,而且不是很直观



如果有像''concat null yield
$这样的设置会有多好b $ b null''用于算术运算符。


任何人都知道如何解决这个问题吗?


谢谢。

Paul

Hi,

I need this behaviour: 1 + null = 1
I have a (dynamic) set of many columns containing decimals that I want
to add as follows:
if all columns are null the result should be null
if not all columns are null, the null columns may be regarded as 0.

E.g.
null + null + 1 = 1
null + null + null = null

The problem is that the first expression yields null.

Up till now I generated an update statement with isnull(<column>,0),
however, then the second expression yields 0.
I can add another update statment setting the result to null if all
columns are null, but this is very slow, and not very intuitive
either.
How nice it would be if there were a setting like ''concat null yields
null'' for arithmetic operators.

Anyone any idea how to fix this?

Thanks.
Paul



COALESCE(Col1,0)+ COALESCE(Col2,0)+ COALESCE(Col3,0)+ COALESCE(Col1,

Col2,Col3)

COALESCE(Col1, 0) + COALESCE(Col2, 0) + COALESCE(Col3, 0) + COALESCE(Col1,
Col2, Col3)


使用coalesce与使用isnull的解决方案相同。它b / b $ b $不符合我的要求。特别是,如果所有输入都为空,结果

将为0。结果必须是

null。

无论如何,谢谢。

Using coalesce is the same sort of solution as using isnull. It
doesn''t behave as my requirements state. In particular, the result
will be 0 if all inputs are null. It is required that the result be
null.
Thanks anyway.


5月29日,4:37 pm,我... @ vanoordt.nl写道:
On May 29, 4:37 pm, i...@vanoordt.nl wrote:

使用coalesce与使用isnull的解决方案相同。它b / b $ b $不符合我的要求。特别是,如果所有输入都为空,结果

将为0。要求结果是

null。

无论如何,谢谢。
Using coalesce is the same sort of solution as using isnull. It
doesn''t behave as my requirements state. In particular, the result
will be 0 if all inputs are null. It is required that the result be
null.
Thanks anyway.



编号您是否测试过

如果全部为空,结果将为空。

因为

COALESCE(Col1,Col2,Col3)返回null并且

0 + 0 + 0 + null为空

COALESCE需要更多参数和ISNULL只有两个


声明@a表(col1 int,col2 int,col3 int)


插入@a值(1,null ,null)

插入@a值(null,2,null)

插入@a值(null,null,3)

插入@a值(1,2,null)

插入@a值(null,2,3)

插入@a值(1,null ,3)

插入@a值(null,null,null)


选择COALESCE(Col1,0)+ COALESCE(Col2,0)+ COALESCE(Col3,0)+

COALESCE(Col1,

Col2,Col3)来自@a

2

4

6

4

7

5

NULL

No. Did you test

Result will be null if all are null .
since
COALESCE(Col1, Col2, Col3) returns null and
0 + 0 + 0 + null is null
COALESCE takes more arguments and ISNULL only two

declare @a table (col1 int,col2 int,col3 int)

insert into @a values (1,null,null)
insert into @a values (null,2,null)
insert into @a values (null,null,3)
insert into @a values (1,2,null)
insert into @a values (null,2,3)
insert into @a values (1,null,3)
insert into @a values (null,null,null)

select COALESCE(Col1, 0) + COALESCE(Col2, 0) + COALESCE(Col3, 0) +
COALESCE(Col1,
Col2, Col3) from @a
2
4
6
4
7
5
NULL


这篇关于需要:1 + null = 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆