Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to convert rows to comma seperated row

Author  Topic 

vision.v1
Yak Posting Veteran

72 Posts

Posted - 2013-04-25 : 11:17:02
Hi,

I have below table in which i have countryCodes but i want the result to return in comma-seperated instead of rows

CREATE TABLE #temp
(
countrycodes INT
)

INSERT #temp SELECT '01'
INSERT #temp SELECT '02'
INSERT #temp SELECT '03'
INSERT #temp SELECT '04'

Current Output
---------------

SELECT countrycodes from #temp

CountryCodes
------------
1
2
3
4

Expected Output
---------------

1,2,3,4

Please advise.

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-25 : 11:43:49
[code]
SELECT STUFF((SELECT ',' + CAST(countrycodes AS varchar(5)) FROM #temp ORDER BY countrycodes FOR XML PATH('')),1,1,'')
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -