Oracle PL/SQL - DECODE 语句
两列都应该使用: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您正在寻找 COALESCE
COALESCE(Customer_num , Vendor_num)
请注意最后这一点要求
将重新运行 Customer_num(因为第一个要求)或当它们都为空时返回 NULL。
I think you're looking for COALESCE
COALESCE(Customer_num , Vendor_num)
Note this last requirement
Will either retrun Customer_num (because of the first requirement) or NULL when they are both null.
我同意其他发帖者的观点,即“更好”的方法是使用 NVL 或 COALESCE。然而,虽然我没想到这会起作用,但似乎以下内容会满足您的要求:
奇怪的是我们可以使用 DECODE 将值与 NULL 进行比较并让它返回(显然)TRUE。在我看来,上面的代码在概念上与
显然 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:
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
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.
您的问题不太清楚。
但您可以尝试使用
nvl
:Your question is not that clear.
But you could try using
nvl
:尝试
Try