SQL Backup Pro
Getting started with SSIS - Part 1: Introduction to SSIS
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

I am working as a BI Developer in Accenture and have around 3+ years of experience. I have contributed many articles on www.sqlservercentral.com and www.mssqltips.com.

Archive · View All
September 2009 3
July 2009 2
March 2011 1
March 2010 1
February 2010 1

Concatenate multiple rows delimited by comma

Jul 29 2009 12:26PM by Divya Agrawal   

You might have heard about people looking for concatenating more than one rows into a csv string. Previously i found out difficult enough. I used coaelesce to do that . But coaelesce usually asks for a varibale to be declared and updates the values in that variable on an iterative basis. But then i tried using XML. It worked pretty well with that using a single query. Most of you might be aware of this, but let me share it for the other who are novice to this.

Say we have a table which has two columns as described in the defination below:

Declare @tbl table (id int,sid int)

id is a primary id and sid is a secondary id which can have multiple values for the same. The idea is to concatenate all the sids of a single id into a single row delimited by comma.

Lets fill the table with some values

insert into @tbl
select 1,11
union
select 1,12
union
select 1,13
union
select 2,21
union
select 2,22

This will insert few rows into the table. Now our first task would be to get all the sids of a single id.

Below query would hep you getting the same:

select distinct id,
(select cast(sid as varchar)+',' from @tbl t where t.id=t1.id for XML path('')) as sid
from @tbl t1

The inner query selected as the second column is using XML to get a comma separated list of rows.

id   sid

1    11,12,13,
2    21,22,

Now, to remove the last comma we can have a replace function as shown:

select distinct id,
replace((select cast(sid as varchar)+',' from @tbl t where t.id=t1.id for XML path(''))+'$',',$','') as sid
from @tbl t1

id   sid

1    11,12,13
2    21,22

I have appended a '$' at the end to identify as the last comma only has to be replaced.

Cool no, see how easily we can create a comma delimited string.

Please do let me know any feed backs.

 

Tags: XML, multiple_rows, Concatenate,


Divya Agrawal
78 · 3% · 471
0
Lifesaver
 
0
Learned
 
0
Incorrect
 
3
Liked
 
1
Refreshed



Submit

6  Comments  

Unsubscribe from Notifications
  • Very Good Post.

    Kind Regards, Pinal

    commented on Jul 29 2009 9:00PM  .  Report Abuse This post is not formatted correctly
    Pinal Dave
    159 · 1% · 204
  • Hi,

    Nice article.

    Adding to your idea, we can also do the same by this way, which will make it simple:

    Declare @tbl table (id int,sid int)

    insert into @tbl select 1,11 union select 1,12 union select 1,13 union select 2,21 union select 2,22

    select distinct id, replace( ( SELECT cast(sid as varchar) as 'data()' from @tbl t where t.id=t1.id for XML path('') ) ,' ',',') as sid from @tbl t1

    Check for more reference: http://www.sqlyoga.com/2009/02/sql-server-get-comma-separated-list.html

    Thanks,

    Tejas http://www.SQLYOGA.com

    commented on Jul 30 2009 1:25AM  .  Report Abuse This post is not formatted correctly
    Tejas Shah
    112 · 2% · 324
  • Thanks for the feedback.. data() part is a new thing for me

    commented on Jul 30 2009 12:41PM  .  Report Abuse This post is not formatted correctly
    Divya Agrawal
    78 · 3% · 471
  • The advantage with the data() is you don't need to convert the numeric values into character string to Concatenate them ... used the sample data provided by Tejas

    Declare @tbl table (id int,sid int)

    insert into @tbl

    select 1,11

    union

    select 1,12

    union

    select 1,13

    union

    select 2,21

    union

    select 2,22

    select distinct id,

    replace(

    (

    SELECT sid as 'data()'

    from @tbl t

    where t.id=t1.id

    for XML path('')

    )

    ,' ',',') as sid

    from @tbl t1

    commented on Aug 4 2009 2:17AM  .  Report Abuse This post is not formatted correctly
    Mangal
    264 · 1% · 103
  • and forgot to add.. disadvantage with data() is, it will fail in case there are spaces in values of column.

    commented on Aug 4 2009 2:25AM  .  Report Abuse This post is not formatted correctly
    Mangal
    264 · 1% · 103
  • Hi there,

    I am way new to SQL and have what I hope is a simple question, but boggles me. I have a file with over 270,000 rows of information. Below is just a sample of the data:

    ParcelNumber,Deedholder,SaleDate,Recording 01-4.0-19.0-000.0-001-001.000,"SCHANZMEYER, ROSS E & LINDA",121/2003,574/249 01-4.0-19.0-000.0-001-001.000,"SCHANZMEYER, ROSS E & LINDA",12/1/2003,574/248 01-4.0-19.0-000.0-001-001.000,"SCHANZMEYER, ROSS E & LINDA",5/1/1987,298/3 01-4.0-19.0-000.0-001-001.000,"SCHANZMEYER, ROSS E & LINDA",7/1/1985,276/45 01-4.0-19.0-000.0-001-001.000,"SCHANZMEYER, ROSS E & LINDA",3/1/2008,659/706 01-4.0-19.0-000.0-001-001.001,"THOMPSON, WILLIAM S JR & NANCY",5/1/1999,478/715 01-4.0-19.0-000.0-001-001.001,"THOMPSON, WILLIAM S JR & NANCY",9/1/1993,386/940 01-4.0-19.0-000.0-001-001.001,"THOMPSON, WILLIAM S JR & NANCY",5/1/1989,324/830

    I am wanting the data to be displayed in one row with the Parcel_Number being the primary key and all my Recording being displayed in columns...any change you might be able to assist me?

    If I just need to take a class please let me know!

    Thank you! Jen

    commented on Nov 29 2011 1:14PM  .  Report Abuse This post is not formatted correctly
    jen
    2173 · 0% · 1

Your Comment


Sign Up or Login to post a comment.

"Concatenate multiple rows delimited by comma " rated 5 out of 5 by 3 readers
Concatenate multiple rows delimited by comma , 5.0 out of 5 based on 3 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising