【问题标题】:DB2: How to concatenate null strings in DB2?DB2:如何在 DB2 中连接空字符串?
【发布时间】:2011-09-29 11:06:50
【问题描述】:

我必须连接 2 列(例如 FIRSTANME 和 LASTNAME)。
我是这样做的:

FIRSTNAME || ' ' || LASTNAME`.   

如果其中一个为空,但另一个不为空,则我得到空作为连接结果。
我想要以下行为

FIRSTNAME = null and LASTNAME = "Smith" ==> 
  FIRSTANME || ' ' || LASTNAME == ' Smith'. 

如何在 DB2 中解决这个问题?

【问题讨论】:

    标签: sql database db2


    【解决方案1】:

    使用coalesce

    ...
    CONCAT( COALESCE(firstname,'') , COALESCE(lastname,'') )
    

    或使用|| concat 运算符

    ...
    COALESCE(firstname,'') || COALESCE(lastname,'') 
    

    请注意,IBM 使用关键字 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

    【讨论】:

      最近更新 更多