DB2 SQL 中的 IsNull 函数?

发布于 2024-07-04 21:40:25 字数 237 浏览 4 评论 0原文

DB2 是否有与 isnull 函数相当的性能?

想象一下我们的一些产品是内部的,因此它们没有名称:

Select product.id, isnull(product.name, "Internal) 
From product

可能会返回:

1 Socks 
2 Shoes 
3 Internal 
4 Pants

Is there a performant equivalent to the isnull function for DB2?

Imagine some of our products are internal, so they don't have names:

Select product.id, isnull(product.name, "Internal) 
From product

Might return:

1 Socks 
2 Shoes 
3 Internal 
4 Pants

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

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

发布评论

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

评论(8

笑着哭最痛 2024-07-11 21:40:26

如果您需要使用 if/else,另一种选择是:

NVL2 (string_to_be_tested, string_if_not_null, string_if_null);

即:

SELECT product.ID, NVL2(product.Name, "Internal", "External") AS ProductName
FROM Product;

Another option, in case you need to use if/else, is:

NVL2 (string_to_be_tested, string_if_not_null, string_if_null);

i.e.:

SELECT product.ID, NVL2(product.Name, "Internal", "External") AS ProductName
FROM Product;
跨年 2024-07-11 21:40:26

希望这可以帮助其他人

  SELECT 
.... FROM XXX XX
WHERE
....
AND(
       param1 IS NULL
       OR XX.param1 = param1
       )

hope this might help someone else out there

  SELECT 
.... FROM XXX XX
WHERE
....
AND(
       param1 IS NULL
       OR XX.param1 = param1
       )
小清晰的声音 2024-07-11 21:40:26

我认为 COALESCE 函数部分类似于 isnull,但请尝试一下。

为什么不通过应用程序使用空处理函数,这是更好的选择。

I think COALESCE function partially similar to the isnull, but try it.

Why don't you go for null handling functions through application programs, it is better alternative.

意中人 2024-07-11 21:40:26

COALESCE 函数与 ISNULL 函数相同
笔记。 您必须使用 COALESCE 函数,该函数与您检查为空的列的数据类型相同。

COALESCE function same ISNULL function
Note. you must use COALESCE function with same data type of column that you check is null.

紫罗兰の梦幻 2024-07-11 21:40:25
Select Product.ID, VALUE(product.Name, "Internal") AS ProductName from Product
Select Product.ID, VALUE(product.Name, "Internal") AS ProductName from Product
对你再特殊 2024-07-11 21:40:25

我对 DB2 不熟悉,但是你尝试过 COALESCE 吗?

IE:


SELECT Product.ID, COALESCE(product.Name, "Internal") AS ProductName
FROM Product

I'm not familiar with DB2, but have you tried COALESCE?

ie:


SELECT Product.ID, COALESCE(product.Name, "Internal") AS ProductName
FROM Product
世界等同你 2024-07-11 21:40:25

DB2中有一个函数NVL(field, value if null)。

示例:

SELECT ID, NVL(NAME, "Internal) AS NAME, NVL(PRICE,0) AS PRICE FROM Product with UR;

In DB2 there is a function NVL(field, value if null).

Example:

SELECT ID, NVL(NAME, "Internal) AS NAME, NVL(PRICE,0) AS PRICE FROM PRODUCT WITH UR;

吃不饱 2024-07-11 21:40:25

就其价值而言,COALESCE 很相似,但却

IFNULL(expr1, default)

是您在 DB2 中寻找的完全匹配的对象。

COALESCE 允许多个参数,返回第一个 NON NULL 表达式,而 IFNULL 只允许表达式和默认值。

因此,为

SELECT product.ID, IFNULL(product.Name, "Internal") AS ProductName
FROM Product

您提供了您正在寻找的内容以及之前的答案,只是为了完整性而添加。

For what its worth, COALESCE is similiar but

IFNULL(expr1, default)

is the exact match you're looking for in DB2.

COALESCE allows multiple arguments, returning the first NON NULL expression, whereas IFNULL only permits the expression and the default.

Thus

SELECT product.ID, IFNULL(product.Name, "Internal") AS ProductName
FROM Product

Gives you what you're looking for as well as the previous answers, just adding for completeness.

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