合并名字和姓氏,并更新为每行的全名列

时间:2016-11-18 05:39:01

标签: sql-server tsql

对于每个名字和姓氏,我只想将它们组合在一起并将它们更新为同名"名称"中的全名列。表。

这应该发生在表格中的每一行。列是Id,FirstName,LastName和FullName。

任何帮助将不胜感激

update Names n 
set n.FullName = (
    select CONCAT(FirstName,' ',LastName)  
    from Names a 
    where n.Id = a.Id
)
where n.FullName is null 
and n.FirstName is not null and n.LastName is not null

1 个答案:

答案 0 :(得分:4)

此更新FullName Colum为空或具有NULL值。

UPDATE Names
SET FullName = ISNULL(FirstName + ' ','') +  ISNULL(LastName,'')
WHERE ISNULL(FullName,'') = '' 

修改

UPDATE Names
SET FullName = FirstName + ' ' + LastName
WHERE ISNULL(FullName,'') = '' AND  ISNULL(FirstName,'') <> '' AND ISNULL(LastName,'') <> ''