Welcome to Hellrocker's Collections

Hope its all of some use to computer users.............

Tuesday, March 22, 2011

Comma separated values from Stored Procedure

You might have stuck in a position where you need all or certain values of a particular column as comma separated string in your application. What do you do in that case?

Well, the generic approach for this is return the DataTable and iterate its column while adding comma after each value and forming a string or

we can read the result in a DataReader and keep reading it till next while following the above mentioned approach.

If the database server performance is not a major issue for you and if you prefer to write less code, in that case...


Here is a quick solution to perform the same at the database level...



Use the following query to get the values in a comma separated string...

SELECT SUBSTRING(
(SELECT ',' + <Column Name>
FROM <Table Name>
FOR XML PATH('')),2,200000) AS <Column Name to be displayed in result>


I hope it is helpful to you in some or the other cases.

0 comments:

Post a Comment