连接并插入表中

时间:2014-11-20 20:28:15

标签: sql-server insert concatenation rows

我的数据库中有一个表,在我的表中我有4列 NAME_1 NAME_2 LASTNAME_1 LASTNAME_2

好了,我现在添加一个列“FULL NAME”,我希望连接NAME_1 NAME_ 2 LASTNAME_1和LASTNAME_2并插入FULL_NAME。

我该怎么办?

NAME_1      NAME_2     LASTNAME_1    LASTNAME_2     
-------------------------------------------------
ALISON     NULL      LAWRENCE      NULL         
CARLOS     NULL      LOPEZ         RECINOS       
MEL        ANDREA    CHEW          PHELPS     



NAME1      NAME2     LASTNAME1    LASTNAME2     FULL_NAME     
----------------------------------------------------------------------
ALISON     NULL      LAWRENCE      NULL        ALISON LAWRENCE    
CARLOS     NULL      LOPEZ         RECINOS    CARLOS LOPEZ RECINOS   
MEL        ANDREA    CHEW          PHELPS     MEL ANDREA CHEW PHELPS   

1 个答案:

答案 0 :(得分:2)

您可以使用ISNULL和连接运算符+并设置值

UPDATE Table1
SET  FULL_NAME = ISNULL(NAME_1,'') + 
                 ISNULL(NAME_2,'') + 
                 ISNULL(LASTNAME_1,'') + 
                 ISNULL(LASTNAME_2,'')