Microsoft SQL:CASE WHEN 与 ISNULL/NULLIF

发布于 2024-07-19 12:32:32 字数 341 浏览 6 评论 0原文

除了可读性之外,在防止 SQL 中出现被 0 错误时,使用 CASE WHEN 语句与 ISNULL/NULLIF 相比还有什么显着的好处吗?

CASE WHEN (BeginningQuantity + BAdjustedQuantity)=0 THEN 0 
ELSE EndingQuantity/(BeginningQuantity + BAdjustedQuantity) END

ISNULL((EndingQuantity)/NULLIF(BeginningQuantity + BAdjustedQuantity,0),0)

Besides readability is there any significant benifit to using a CASE WHEN statement vs ISNULL/NULLIF when guarding against a divide by 0 error in SQL?

CASE WHEN (BeginningQuantity + BAdjustedQuantity)=0 THEN 0 
ELSE EndingQuantity/(BeginningQuantity + BAdjustedQuantity) END

vs

ISNULL((EndingQuantity)/NULLIF(BeginningQuantity + BAdjustedQuantity,0),0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

手心的海 2024-07-26 12:32:32

请记住,NULL 与 0 不同。因此问题中的两个代码片段对于相同的输入可能会返回不同的结果。

例如,如果 BeginningQuantity 为 NULL,则第一个表达式的计算结果为 NULL:

CASE WHEN (NULL + ?)=0 THEN 0 ELSE ?/(NULL + ?) END

现在 (NULL + ?) 等于 NULL,并且 NULL=0 为 false,因此计算 ELSE 子句,给出 ?/(NULL+?),结果为 NULL 。 然而,第二个表达式变成:

ISNULL((?)/NULLIF(NULL + ?,0),0)

Here NULL+? 变为 NULL,并且由于 NULL 不等于 0,因此 NULLIF 返回第一个表达式,该表达式为 NULL。 外部 ISNULL 捕捉到这一点并返回 0。

因此,请下定决心:您是要防止除数为零,还是除数 NULL? ;-)

Remember that NULL is different from 0. So the two code snippets in the question can return different results for the same input.

For example, if BeginningQuantity is NULL, the first expression evaluates to NULL:

CASE WHEN (NULL + ?)=0 THEN 0 ELSE ?/(NULL + ?) END

Now (NULL + ?) equals NULL, and NULL=0 is false, so the ELSE clause is evaluated, giving ?/(NULL+?), which results in NULL. However, the second expression becomes:

ISNULL((?)/NULLIF(NULL + ?,0),0)

Here NULL+? becomes NULL, and because NULL is not equal to 0, the NULLIF returns the first expression, which is NULL. The outer ISNULL catches this and returns 0.

So, make up your mind: are you guarding against divison by zero, or divison by NULL? ;-)

做个少女永远怀春 2024-07-26 12:32:32

在你的例子中,我认为性能可以忽略不计。 但在其他情况下,根据除数的复杂性,答案是“视情况而定”。

这是一个有趣的博客 关于主题:

为了可读性,我喜欢案例/时间。

In your example I think the performance is negligible. But in other cases, depending on the complexity of your divisor, the answer is 'it depends'.

Here is an interesting blog on the topic:

For readability, I like the Case/When.

绿萝 2024-07-26 12:32:32

在我看来,使用 Isnull/Nullif 比使用 Case When 更快。 我更喜欢 isnull/nullif。

In my opinion, using Isnull/Nullif is faster than using Case When. I rather the isnull/nullif.

瞳孔里扚悲伤 2024-07-26 12:32:32

我会使用 ISNULL,但尝试对其进行格式化,以便它更好地显示含义:

SELECT
    x.zzz
        ,x.yyyy
        ,ISNULL(
                   EndingQuantity / NULLIF(BeginningQuantity+BAdjustedQuantity,0)
                ,0)
        ,x.aaa
    FROM xxxx...

I would use the ISNULL, but try to format it so it shows the meaning better:

SELECT
    x.zzz
        ,x.yyyy
        ,ISNULL(
                   EndingQuantity / NULLIF(BeginningQuantity+BAdjustedQuantity,0)
                ,0)
        ,x.aaa
    FROM xxxx...
高速公鹿 2024-07-26 12:32:32
CASE WHEN (coalesce(BeginningQuantity,0) + coalesce(BAdjustedQuantity,0))=0 THEN 0 ELSE coalesce(EndingQuantity,0)/(coalesce(BeginningQuantity,0) + coalesce(BAdjustedQuantity,0)) END

恕我直言,你最好的选择

CASE WHEN (coalesce(BeginningQuantity,0) + coalesce(BAdjustedQuantity,0))=0 THEN 0 ELSE coalesce(EndingQuantity,0)/(coalesce(BeginningQuantity,0) + coalesce(BAdjustedQuantity,0)) END

your best option imho

鸠书 2024-07-26 12:32:32

抱歉,这是更简化的 SQL 查询。

SELECT 

(ISNULL([k1],0) + ISNULL([k2],0)) /

CASE WHEN (
(
   CASE WHEN [k1] IS NOT NULL THEN 1 ELSE 0 END +
   CASE WHEN [k2] IS NOT NULL THEN 1 ELSE 0 END
) > 0 )
THEN
(
  CASE WHEN [k1] IS NOT NULL THEN 1 ELSE 0 END +
  CASE WHEN [k2] IS NOT NULL THEN 1 ELSE 0 END
)
ELSE 1 END

FROM dbo.[Table]

Sorry, here is the little more simplify upbuilded sql query.

SELECT 

(ISNULL([k1],0) + ISNULL([k2],0)) /

CASE WHEN (
(
   CASE WHEN [k1] IS NOT NULL THEN 1 ELSE 0 END +
   CASE WHEN [k2] IS NOT NULL THEN 1 ELSE 0 END
) > 0 )
THEN
(
  CASE WHEN [k1] IS NOT NULL THEN 1 ELSE 0 END +
  CASE WHEN [k2] IS NOT NULL THEN 1 ELSE 0 END
)
ELSE 1 END

FROM dbo.[Table]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文