SQL 与 NULL 值相加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1769648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:30:29  来源:igfitidea点击:

Addition with NULL values

sqlnull

提问by Thorsten

In a stored procedure (Oracle in my case), I want to add some values to an existing record. Problem is that both the existing value and the value to be added can be null. I only want the result to be NULL when both operands are null. If only one of them is null, I want the result to be the other operand. If both are non-null, I want the result to be "normal" addition.

在存储过程(在我的例子中是 Oracle)中,我想向现有记录添加一些值。问题是现有值和要添加的值都可以为空。当两个操作数都为空时,我只希望结果为 NULL。如果其中只有一个为空,我希望结果是另一个操作数。如果两者都不为空,我希望结果是“正常”加法。

Here's what I am using so far:

这是我目前使用的:

SELECT column INTO anz_old FROM aTable Where <someKeyCondition>;
IF anz_old IS NULL
THEN
    anz_new := panzahl;
ELSE
    anz_new := anz_new + NVL (panzahl, 0);
END IF;
UPATE aTabel set column = anz_new Where <someKeyCondition>;

Is there a more elegant way (pereferably completely in SQL, i.e. just in an update statement short of a long CASE-Statement with basically the same logic as the above code)?

有没有更优雅的方法(最好完全在 SQL 中,即只是在更新语句中缺少长 CASE-Statement 的逻辑与上述代码基本相同)?

回答by rjmunro

If you want to add a and b and either may be null, you could use coalesce, which returns the first non-null parameter you pass it:

如果你想添加 a 和 b 并且其中一个可能为空,你可以使用coalesce,它返回你传递的第一个非空参数:

coalesce(a+b, a, b)

So in this case, if neither parameter is null, it will return the sum. If only b is null, it will skip a+b and return a. If a is null, it will skip a+b and a and return b, which will only be null if they are both null.

所以在这种情况下,如果两个参数都不为空,它将返回总和。如果只有 b 为空,它将跳过 a+b 并返回 a。如果 a 为空,它将跳过 a+b 和 a 并返回 b,只有当它们都为空时才会为空。

If you want the answer to be 0 rather than null if both a and b are null, you can pass 0 as the last parameter:

如果在 a 和 b 都为空的情况下希望答案为 0 而不是 null,则可以将 0 作为最后一个参数传递:

coalesce(a+b, a, b, 0)

Do consider @erwins answer- nullmight not be the right thing to be using.

请考虑@erwins 的答案-null可能不是正确的使用方法。

回答by sean

I accomplished it this way:

我是这样实现的:

coalesce("Column1",0.00) + coalesce("Column2",0.00)

I'm working with front end high level execs.... They don't understand why NULL and 0 aren't handled the same way.

我正在与前端高级执行官一起工作......他们不明白为什么 NULL 和 0 的处理方式不同。

In my case it works, just replacing NULLs with 0.00... may not in all though :)

在我的情况下,它可以工作,只是用 0.00 替换 NULL ......虽然可能不是全部:)

回答by user734028

You can also use ISNULL, so if you have 3 values

你也可以使用 ISNULL,所以如果你有 3 个值

isnull(val1,0)+isnull(val2,0)+isnull(val3,0)

which ever column will have a NULL will use a 0, otherwise its original value.

哪一列将有一个 NULL 将使用 0,否则它的原始值。

回答by Erwin Smout

In SQL, Null is supposed to be a state that says "I don't know".

在 SQL 中,Null 应该是一个表示“我不知道”的状态。

If you don't know how much b is, then you also do not know how much a+b is, and it is misleading to pretend that a+b=a in that case.

如果你不知道 b 是多少,那么你也不知道 a+b 是多少,在这种情况下假装 a+b=a 是一种误导。

回答by justasqluser

In SQL terms, when adding numbers, a result of NULL means there were no non-null numbers added.

在 SQL 术语中,当添加数字时,结果为 NULL 意味着没有添加非空数字。

This suggests that a sensible answer in SQL terms would be

这表明 SQL 术语中的明智答案是

CASE WHEN A IS NULL AND B IS NULL THEN NULL ELSE ISNULL(A, 0) + ISNULL(B, 0) END

当 A 为 NULL 且 B 为 NULL 时为 NULL 则为 NULL ELSE ISNULL(A, 0) + ISNULL(B, 0) END