SQL DB2:如何在 DB2 中连接空字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7596425/
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
DB2: How to concatenate null strings in DB2?
提问by adrift
I have to concatenate 2 columns (ex. FIRSTANME and LASTNAME).
I do it this way:
我必须连接 2 列(例如 FIRSTANME 和 LASTNAME)。
我这样做:
FIRSTNAME || ' ' || LASTNAME`.
If one of them is null, but the other one is not null, I get null as concatenation result.
And I want following behavior
如果其中一个为空,而另一个不为空,则我将获得空作为连接结果。
我想要以下行为
FIRSTNAME = null and LASTNAME = "Smith" ==>
FIRSTANME || ' ' || LASTNAME == ' Smith'.
How to solve this in DB2?
如何在 DB2 中解决这个问题?
回答by Johan
Use coalesce
用 coalesce
...
CONCAT( COALESCE(firstname,'') , COALESCE(lastname,'') )
Or using the ||
concat operator
或者使用||
concat 运算符
...
COALESCE(firstname,'') || COALESCE(lastname,'')
Note that IBM recomments using the keyword concat
and not the ||
operator.
请注意,IBM 建议使用关键字concat
而不是||
运算符。
Concat: http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Ffconc.htm
Coalesce: http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Ffcoal.htm
CONCAT:http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Ffconc.htm
合并:HTTP://publib.boulder。 ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=%2Fcom.ibm.db2.doc.sqlref%2Ffcoal.htm