Oracle PL/SQL - DECODE 语句

发布于 2024-10-12 11:15:35 字数 297 浏览 1 评论 0原文

两列都应该使用:Customer_num 是第一个,但如果为空,则使用 Vendor_num。

更具体地说,我需要编写一个 DECODE 语句,该语句将返回以下内容:

If Customer_num IS NOT NULL then return Customer_num 如果 Customer_num 为 NULL,则返回 Vendor_num 如果 Vendor_num IS NULL 或如果 Customer_num IS NULL,则返回 Customer_num

我该如何执行此操作?

谢谢...

Both columns should be used: Customer_num is first, but if null, then use the Vendor_num.

To be more specific, I need to write a DECODE statement that will return the following:

If Customer_num IS NOT NULL then return Customer_num
If Customer_num IS NULL then return Vendor_num
If Vendor_num IS NULL Or If Customer_num IS NULL then return Customer_num

How can I do this?

Thanks...

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

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

发布评论

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

评论(4

月野兔 2024-10-19 11:15:35

我认为您正在寻找 COALESCE

COALESCE(Customer_num , Vendor_num)

请注意最后这一点要求

如果 Vendor_num IS NULL 或如果 Customer_num IS NULL,则返回 Customer_num

将重新运行 Customer_num(因为第一个要求)或当它们都为空时返回 NULL。

I think you're looking for COALESCE

COALESCE(Customer_num , Vendor_num)

Note this last requirement

If Vendor_num IS NULL Or If Customer_num IS NULL then return Customer_num

Will either retrun Customer_num (because of the first requirement) or NULL when they are both null.

遮了一弯 2024-10-19 11:15:35

我同意其他发帖者的观点,即“更好”的方法是使用 NVL 或 COALESCE。然而,虽然我没想到这会起作用,但似乎以下内容会满足您的要求:

SELECT DECODE(CUSTOMER_NUM,
                 NULL, DECODE(VENDOR_NUM,
                                NULL, CUSTOMER_NUM,
                                      VENDOR_NUM),
                       CUSTOMER_NUM)
  FROM DUAL;

奇怪的是我们可以使用 DECODE 将值与 NULL 进行比较并让它返回(显然)TRUE。在我看来,上面的代码在概念上与

IF CUSTOMER_NUM = NULL THEN
  IF VENDOR_NUM = NULL THEN
    RETURN CUSTOMER_NUM;
  ELSE
    RETURN VENDOR_NUM;
  END IF;
ELSE
  RETURN CUSTOMER_NUM;
END;

显然 DECODE 足够明亮,可以执行与 NULL 的比较,就好像使用了 IS NULL 子句一样。感谢您提出这个有趣的小问题。

I agree with the other posters that the "better" way to do this is to use NVL or COALESCE. However, although I didn't expect this to work it appears that the following will do what you asked:

SELECT DECODE(CUSTOMER_NUM,
                 NULL, DECODE(VENDOR_NUM,
                                NULL, CUSTOMER_NUM,
                                      VENDOR_NUM),
                       CUSTOMER_NUM)
  FROM DUAL;

What's odd is that we can use DECODE to compare a value to NULL and have it return (apparently) TRUE. In my mind the code above is conceptually the same as

IF CUSTOMER_NUM = NULL THEN
  IF VENDOR_NUM = NULL THEN
    RETURN CUSTOMER_NUM;
  ELSE
    RETURN VENDOR_NUM;
  END IF;
ELSE
  RETURN CUSTOMER_NUM;
END;

Apparently DECODE is bright enough to perform comparisons to NULL as if an IS NULL clause was used. Thanks for posing this interesting little problem.

友谊不毕业 2024-10-19 11:15:35

您的问题不太清楚。

但您可以尝试使用 nvl

select nvl(customer_num, vendor_num)
  from your_table;

Your question is not that clear.

But you could try using nvl:

select nvl(customer_num, vendor_num)
  from your_table;
倚栏听风 2024-10-19 11:15:35

尝试

 SELECT coalesce( expr1, expr2, ... expr_n )
 FROM DUAL;

Try

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