将2行连接到一行

时间:2015-04-20 13:30:08

标签: sql-server sql-server-2008-r2 concatenation

我有两行数据,我想将它们组合起来像

enter image description here

  

147000.0000000,49000.00000

之前我尝试连接两个产生这样的列,enter image description here

如何将两行合并为一行?


1 个答案:

答案 0 :(得分:2)

您可以像这样使用FOR XML PATH

<强>查询

;WITH Table1 as 
(
SELECT 147000.0000000 as TDS_INAMOUNT
UNION ALL SELECT 49000.00000
)
SELECT 
STUFF((SELECT ',' + CONVERT(VARCHAR(30),TDS_INAMOUNT)
FROM Table1
FOR XML PATH(''),TYPE
).value('.','VARCHAR(2000)'),1,1,'')

<强>输出

147000.0000000 , 49000.00000

如果您想要输出

147000.0000000 in ASKARI HIGH YIELD SCHEME,49000.0000000 in ASKARI EQUITY FUND

使用此功能。

SELECT 
STUFF((SELECT ',' + CONVERT(VARCHAR(30),TDS_INAMOUNT) + ' in ' + PRD_Name
FROM Table1
FOR XML PATH(''),TYPE
).value('.','VARCHAR(2000)'),1,1,'')