Convert Table To Comma Delimited String In SQL Server 2008

Introduction

Its very often to convert a database table to comma delimited string for various purposes while working in SQL Server. Here I will give an example to convert a database table to a comma delimited string.





Behind The Scene

Before going to use the script that I will provide here you need to have some basic idea about the keyword STUFF ( Transaction SQL ).

The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.

STUFF ( character_expression , start , length , replaceWith_expression )

character_expression

    Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data.

start

    Is an integer value that specifies the location to start deletion and insertion. If start or length is negative, a null string is returned. If start is longer than the first character_expression, a null string is returned. start can be of type bigint.

length

    Is an integer that specifies the number of characters to delete. If length is longer than the first character_expression, deletion occurs up to the last character in the last character_expression. length can be of type bigint.






replaceWith_expression

Is an expression of character data. character_expression can be a constant, variable, or column of either character or binary data. This expression will replace length characters of character_expression beginning at start.

Here use the query to convert the table to comma separated string value. Here the table used is 'YourtableName' replace it with the required table name, and the column name is 'Name'.

SELECT ComaDelimitedString = STUFF(( SELECT ',' + CAST(Name AS NVARCHAR(max))  
                  FROM YourTableName
                                    FOR XML PATH ('')  
                                     ),1,1,'')

Happy Coding...

8 comments:

  1. With havin so much written content do you ever run into any problems
    of plagorism or copyright violation? My blog has
    a lot of unique content I've either created myself or outsourced but it looks like a lot of it is popping it up all over the web without my permission. Do you know any solutions to help protect against content from being stolen? I'd
    really appreciate it.

    Here is my web-site http://beyondraspberryketones.com

    ReplyDelete
  2. If some one wants expert view regarding running a blog then i propose
    him/her to visit this web site, Keep up the nice job.

    Feel free to visit my webpage Raspberry Ketones

    ReplyDelete
  3. Amazing! Its actually awesome post, I have got much clear idea regarding from this piece
    of writing.

    Here is my website - Raspberry Ketone Health

    ReplyDelete
  4. Greetings from Colorado! I'm bored to death at work so I decided to check out your website on my iphone during lunch break. I really like the information you provide here and can't wait to take a look when I get
    home. I'm amazed at how fast your blog loaded on my phone .. I'm not even using WIFI, just
    3G .. Anyhow, superb site!

    Here is my site - please click the up coming article

    ReplyDelete
  5. Great blog! Do you have any suggestions for aspiring writers?
    I'm planning to start my own website soon but I'm a little lost on everything.
    Would you advise starting with a free platform like Wordpress or go for a paid option?

    There are so many options out there that I'm completely overwhelmed .. Any recommendations? Kudos!

    Review my web-site: http://conf-lose.keul.fr/wiki/index.php?title=Utilisateur:BradyKJS

    ReplyDelete
  6. Howdy, i read your blog occasionally and i own a similar one and i was
    just curious if you get a lot of spam remarks?
    If so how do you prevent it, any plugin or anything you can advise?
    I get so much lately it's driving me crazy so any support is very much appreciated.

    Feel free to visit my web blog solar energy advantages

    ReplyDelete
  7. This site was... how do I say it? Relevant!! Finally I have found something which helped me.
    Many thanks!

    my homepage :: average cost of solar water heater

    ReplyDelete
  8. Great post. I will be dealing with some of these issues as well..


    Have a look at my web page google (google.com)

    ReplyDelete