1

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 :)

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Deviprasad Das
  • 4,203
  • 7
  • 36
  • 51
  • 1
    Sorry i forgot to tell that i want to do it without using any stored procedure. – Deviprasad Das Jul 30 '10 at 13:34
  • How to do this for multiple columns like this http://stackoverflow.com/questions/10037777/send-dbmail-from-sql-server-2000-with-tabular-structured-data – Zeus Apr 06 '12 at 05:15

3 Answers3

3

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 Maccaferri
  • 100,159
  • 46
  • 371
  • 480
2

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

del.ave
  • 1,888
  • 5
  • 17
  • 23
2

Here is one way, There is a student table having studentName column with datatype as nvarchar(50) then following query will give you student names as comma separated values,

DECLARE @VALUES NVARCHAR(1000)
SELECT @VALUES = COALESCE(@VALUES + ',','') + CAST(STUDENTNAME AS NVARCHAR(50)) FROM STUDENT
SELECT @VALUES
Leo
  • 37,640
  • 8
  • 75
  • 100
Jagan
  • 36
  • 1