如何将4列数据合并为一列?

时间:2013-10-22 15:04:06

标签: sql sql-server sql-server-2008

在SQL 2008中,我试图将来自4列的数据合并到一个列中。我搜索并尝试了几种不同的东西,但都没有。我的最新尝试是:

SELECT [2012 Notes] + [2012 STEPS TAKEN TO REMEDY ISSUES] + [2013 NOTES] + [2013 STEPS TAKEN TO REMEDY ISSUES] AS 'ConcatNotes'
FROM ECRSurvey

UPDATE ECRSurvey Set UserNotes = 'ConcatNotes'

但这不起作用。有人能告诉我这样做的正确方法吗?我基本上试图采用4列并尝试将这些数据组合到UserNotes字段中。

3 个答案:

答案 0 :(得分:2)

这应该有效 - 你可能有一些NULLS。

尝试

SELECT ISNULL([2012 Notes],'') + ISNULL([2012 STEPS TAKEN TO REMEDY ISSUES],'') + ISNULL([2013 NOTES],'') + ISNULL([2013 STEPS TAKEN TO REMEDY ISSUES],'') AS 'ConcatNotes'
FROM ECRSurvey

或更新

UPDATE ECRSurvey Set UserNotes = ISNULL([2012 Notes],'') + ISNULL([2012 STEPS TAKEN TO REMEDY ISSUES],'') + ISNULL([2013 NOTES],'') + ISNULL([2013 STEPS TAKEN TO REMEDY ISSUES],'')

答案 1 :(得分:1)

如果其中任何列包含NULL,则结果将为NULL。你会想要对这些字段进行合并或者是无效的检查。

答案 2 :(得分:0)

请尝试以下查询:

Update ECRSurvey
set UserNotes =[2012 Notes] + [2012 STEPS TAKEN TO REMEDY ISSUES] + [2013 NOTES] + [2013 STEPS TAKEN TO REMEDY ISSUES]