是否有与 DB2 的 isnull 函数等效的性能?
想象一下我们的一些产品是内部的,所以它们没有名字:
Select product.id, isnull(product.name, "Internal)
From product
可能返回:
1 Socks
2 Shoes
3 Internal
4 Pants
就其价值而言,COALESCE 是相似的,但
IFNULL(expr1, default)
是您在 DB2 中寻找的完全匹配。
COALESCE 允许多个参数,返回第一个 NON NULL 表达式,而 IFNULL 只允许表达式和默认值。
因此
SELECT product.ID, IFNULL(product.Name, "Internal") AS ProductName
FROM Product
为您提供您正在寻找的内容以及以前的答案,只是为了完整性而添加。
在 DB2 中有一个函数 NVL(field, value if null)。
例子:
SELECT ID, NVL(NAME, "Internal) AS NAME, NVL(PRICE,0) AS PRICE FROM PRODUCT WITH UR;
我对 DB2 不熟悉,但是您尝试过 COALESCE 吗?
IE:
SELECT Product.ID, COALESCE(product.Name, "Internal") AS ProductName
FROM Product
Select Product.ID, VALUE(product.Name, "Internal") AS ProductName from Product
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.
COALESCE
function same ISNULL
function
Note. you must use COALESCE
function with same data type of column that you check is null.
希望这可以帮助其他人
SELECT
.... FROM XXX XX
WHERE
....
AND(
param1 IS NULL
OR XX.param1 = param1
)
如果您需要使用 if/else,另一种选择是:
NVL2 (string_to_be_tested, string_if_not_null, string_if_null);
IE:
SELECT product.ID, NVL2(product.Name, "Internal", "External") AS ProductName
FROM Product;