Convert multiple rows to one row separated by delimiter

  • Hi,

    I am running sqlserver 2012.

    My output data is like

    COL1

    aaa

    bbb

    ccc

    Now, i want to convert the output to

    aaa;bbb;ccc

    How can i do this?

  • Read the following article to find out how to do it.

    http://www.sqlservercentral.com/articles/comma+separated+list/71700/

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • create table #TestData (Col1 varchar(20))

    insert into #TestData

    select 'aaa' union all

    select 'bbb' union all

    select 'ccc'

    select stuff((select ','+Col1

    from #TestData

    order by col1

    for xml path(''))

    ,1,1,'') as delimitedlist

    Edited:

    Whups, you were too quick for me Luis. Yes, that article is a better reference.

    __________________________________________________

    Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
    Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply