DB2 SQL 中的 IsNull 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/65071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IsNull function in DB2 SQL?
提问by Dave
Is there a performant equivalent to the isnull function for DB2?
是否有与 DB2 的 isnull 函数等效的性能?
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
回答by MadMurf
For what its worth, COALESCE is similiar but
就其价值而言,COALESCE 是相似的,但
IFNULL(expr1, default)
is the exact match you're looking for in DB2.
是您在 DB2 中寻找的精确匹配。
COALESCE allows multiple arguments, returning the first NON NULL expression, whereas IFNULL only permits the expression and the default.
COALESCE 允许多个参数,返回第一个 NON NULL 表达式,而 IFNULL 只允许表达式和默认值。
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.
为您提供您正在寻找的内容以及之前的答案,只是为了完整性而添加。
回答by Md. Kamruzzaman
In DB2 there is a function NVL(field, value if null).
在 DB2 中有一个函数 NVL(field, value if null)。
Example:
例子:
SELECT ID, NVL(NAME, "Internal) AS NAME, NVL(PRICE,0) AS PRICE FROM PRODUCT WITH UR;
SELECT ID, NVL(NAME, "Internal) AS NAME, NVL(PRICE,0) AS PRICE from PRODUCT WITH UR;
回答by Chris Shaffer
I'm not familiar with DB2, but have you tried COALESCE?
我不熟悉 DB2,但您是否尝试过 COALESCE?
ie:
IE:
SELECT Product.ID, COALESCE(product.Name, "Internal") AS ProductName
FROM Product
回答by Jnn
Select Product.ID, VALUE(product.Name, "Internal") AS ProductName from Product
回答by turnmoil
hope this might help someone else out there
希望这可以帮助其他人
SELECT
.... FROM XXX XX
WHERE
....
AND(
param1 IS NULL
OR XX.param1 = param1
)
回答by Fuangwith S.
COALESCE
function same ISNULL
function
Note. you must use COALESCE
function with same data type of column that you check is null.
COALESCE
功能相同的 ISNULL
功能注。您必须使用COALESCE
与您检查为空的列的相同数据类型的函数。
回答by venkatram
I think COALESCE
function partially similar to the isnull
, but try it.
我认为COALESCE
功能部分类似于isnull
,但试试看。
Why don't you go for null handling functions through application programs, it is better alternative.
为什么不通过应用程序去使用空处理函数,它是更好的选择。