views:

68

answers:

3

I am getting data like

Result
------
 10
 23 
 21

But i want to get data in the following format.

Result
------
10, 23, 21

How to get that in a Query? Thanks in advance for any help :)

+1  A: 

Sample code that doesn't use stored procedure :) ...

USE AdventureWorks
GO
-- Check Table Column
SELECT Name
FROM HumanResources.Shift
GO
-- Get CSV values
SELECT SUBSTRING(
(SELECT ',' + s.Name
FROM HumanResources.Shift s
ORDER BY s.Name
FOR XML PATH('')),2,200000) AS CSV
GO

More about it here:

SQL SERVER – Comma Separated Values (CSV) from Table Column

Edit:

For SQL-Server 2000, take a look here:

How to Format Query Result as Comma Separated Values (CSV)

Leniel Macaferi
I think XML PATH is not supported in SQL Server 2000 :(
Dev
You're right. It's not supported. http://www.mssqltips.com/tip.asp?tip=1077. For SQL-Server 2000, see the edited answer.
Leniel Macaferi
+2  A: 

You can also check out: http://www.sqlteam.com/article/using-coalesce-to-build-comma-delimited-string

del.ave
A: 

In PostgreSQL:

SELECT company_id, array_to_string(array_agg(employee), ',') FROM mytable GROUP BY company_id;

from http://stackoverflow.com/questions/43870/how-to-concatenate-strings-of-a-string-field-in-a-postgresql-group-by-query

The question is tagged `sql-server-2000`, so PostgreSQL methods will not work (unless they are more generic/standard than this one. You should probably edit or delete this answer.
Brock Adams