2011-09-29 5 views
7

Devo concatenare 2 colonne (ad esempio FIRSTANME e LASTNAME).
lo faccio in questo modo:DB2: come concatenare stringhe null in DB2?

FIRSTNAME || ' ' || LASTNAME`. 

Se uno di loro è nullo, ma l'altro non è nullo, ottengo null come risultato concatenazione.
E voglio seguente comportamento

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

come risolvere questo in DB2?

risposta

13

Usa coalesce

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

o utilizzando l'operatore || concat

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

noti che recomments IBM utilizzando la parola chiave concat e non l'operatore ||.

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