2014-03-19 83 views
1

如何在SQL Server中分隔列值逗號。下面顯示的是我的sql查詢#如何獲取列值逗號分開

select BandName from BandMaster where BandId<100 

我需要在單個查詢中分別獲取BandName值逗號。

+1

的可能重複[如何使它們之間用逗號的T-SQL的結果列表?(http://stackoverflow.com/questions/9726660/how-to-make-a- list-of-t-sql-results-with-commas-between-them) –

+0

任何特定版本的SQL Server? –

+0

還有一個重複的:http://stackoverflow.com/questions/12231439/need-faster-concat-of-columns-in-sql-server-table –

回答

1
Use XML path for this. 

select (select BandName+',' from BandMaster where BandId<100 for xml path('')) as NewColumnName 

Try this 
+0

它將以XML格式返回而不是逗號分離。 – AK47

+0

它將返回逗號分隔。請試用,然後發表評論 – Kailas

+0

是的朋友,我已經試過&然後評論。它是這樣的comminig A B。我錯過了什麼? – AK47

0

試試這個

SELECT BandName = 
       STUFF ((SELECT ','+InrTab.BandName 
         FROM [Table1] InrTab 
         WHERE InrTab.BandId= OutTab.BandId 
              AND InrTab.BandId < 100      
         ORDER BY InrTab.BandId 
         FOR XML PATH(''),TYPE 
         ).value('.','VARCHAR(MAX)') 
         , 1,1,SPACE(0)) 
FROM [Table1] OutTab 
GROUP BY OutTab.BandId; 
0

請試試這個

SELECT GROUP_CONCAT(BandName) from BandMaster where BandId<100

+0

SQL中沒有GROUP_CONCAT SEREVR –

0
Declare @retStr varchar(max) = '' 
select @retStr = @retStr + BandName + ',' from BandMaster 
where BandId<100 

Select @retStr 
相關問題