C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » SQL »

SQL, Select values as CSV in output paramter


Posted Date: 20 Oct 2009    Resource Type: Code Snippets    Category: SQL
Author: hiteshMember Level: Gold    
Rating: 1 out of 5Points: 5



Let say you have a table

Table: FruitBasket
-----------------
ID FruitName
------------------
1 Apple
2 Mango
3 Banana
4 Grapes
5 Orange
-------------------

And you want to have them selected as comma separated values in your output paramters. Below is the quickest method



Declare @csvFruits Nvarchar(max)

Select @csvFruits = COALESCE( @csvFruits + ', ','') + FruitName From FruitBasket

Print @csvFruits



Output:
Apple,Mango,Banana,Grapes,Orange

You can build a user defined function and reuse the function easily.

COALESCE( @csvFruits + ', ','') is similar to ISNULL() function in SQL , read this article.

http://msdn.microsoft.com/en-us/library/ms190349.aspx





Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
SQL select values into CSV string  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource:

Difference between Rank() and Dense_Rank() in SQL

Previous Resource: How to Execute Stored Procedure
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use