Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
MS SQL 2008
Table1
CityColumn

Mumbai
Banglore
Chennai
Colcutta
Delhi
Ahmedabad

Ccan any one get the answer as follows comma separated
OutpoutCol1
Mumbai,Banglore,Chennai,Colcutta,Delhi,Ahmedabad
Posted
Updated 9-Nov-11 21:53pm
v2
Comments
Pandya Anil 10-Nov-11 3:52am    
I found the solution here
http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/

Try this:
SQL
DECLARE @Cities VARCHAR(8000)

SELECT @Cities = ISNULL(@Cities + ',', '') + CityColumn
FROM dbo.Table1

SELECT @Cities AS OutputCol1
GO
 
Share this answer
 
Comments
Pandya Anil 10-Nov-11 4:08am    
thanks... my5
You can also use COALESCE for this.
SQL
DECLARE @myCity VARCHAR(MAX)

SELECT @myCity = COALESCE(@myCity +',' ,'') + CityColumn
FROM Table1
SELECT @myCity
--@myCity will have the values as

--Mumbai,Banglore,Chennai,Colcutta,Delhi,Ahmedabad
 
Share this answer
 
Comments
Amir Mahfoozi 10-Nov-11 4:04am    
+5
Pandya Anil 10-Nov-11 4:08am    
thanks... my5!
I found the solution here
http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900