【问题标题】:Concatenate the values in column "Value1 - Value9" in SQL Server?连接 SQL Server 中“Value1 - Value9”列中的值?
【发布时间】:2019-10-31 06:22:16
【问题描述】:

“dbo.SampleTable”中的数据采用以下格式,其中除 PersonID 之外的所有列都具有 varchar 数据类型,而 PersonID 为 int:

连接“value1 to value3”列中所有值后的预期输出:

输入表中的数据如何转换为输出表没有限制。

可以创建一个新表作为输出表

【问题讨论】:

  • 为什么要这样做?这是某种任务吗?
  • 看看STRING_AGG
  • string_agg 没有为 Value1 和 Value2 列提供所需的结果。添加空处理程序(ISNULL)后,我在值 2 列中得到“Lorem;Ipsum”或“XYZ;BLANK;BLANK”

标签: sql-server concatenation


【解决方案1】:

select DISTINCT Personid,STUFF((SELECT ','+Value1 from #temp t where t.Personid = tt.Personid FOR XML PATH ('')),1,1,'') ,STUFF((SELECT ','+Value2 from #temp t where t.Personid = tt.Personid FOR XML PATH ('')),1,1,'') ,STUFF((SELECT ','+Value3 from #temp t where t.Personid = tt.Personid FOR XML PATH ('')),1,1,'') 来自#temp tt

【讨论】:

  • 不,string_agg 提供了更好的结果,但不是所需的输出
猜你喜欢
  • 2015-03-26
  • 1970-01-01
  • 2016-03-01
  • 2016-04-20
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
  • 1970-01-01
  • 2019-01-07
相关资源
最近更新 更多