如何在SQL Server中分隔列值逗號。下面顯示的是我的sql查詢#如何獲取列值逗號分開
select BandName from BandMaster where BandId<100
我需要在單個查詢中分別獲取BandName值逗號。
如何在SQL Server中分隔列值逗號。下面顯示的是我的sql查詢#如何獲取列值逗號分開
select BandName from BandMaster where BandId<100
我需要在單個查詢中分別獲取BandName值逗號。
試試這個
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;
請試試這個
SELECT GROUP_CONCAT(BandName) from BandMaster where BandId<100
。
SQL中沒有GROUP_CONCAT SEREVR –
Declare @retStr varchar(max) = ''
select @retStr = @retStr + BandName + ',' from BandMaster
where BandId<100
Select @retStr
的可能重複[如何使它們之間用逗號的T-SQL的結果列表?(http://stackoverflow.com/questions/9726660/how-to-make-a- list-of-t-sql-results-with-commas-between-them) –
任何特定版本的SQL Server? –
還有一個重複的:http://stackoverflow.com/questions/12231439/need-faster-concat-of-columns-in-sql-server-table –