SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Concatenating field values in one string


Concatenating field values in one string

Author
Message
Bruce W Cassidy
Bruce W Cassidy
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1317 Visits: 1033
Lynn Pettis (3/19/2009)
Tell me, please, did it hurt? ;-)

Well, it worked. It wasn't terribly quick though. And nor was it dynamic. I had to do a count of the maximum number of lines, then add a couple "just in case", and then a whole string of isnull([#], '')s...

It wasn't pretty.

The FOR XML though (which I have tested, and works well) is fast! :-D

One word of warning: the FOR XML will also transform some control characters (such as a carriage return) into the form '& # x 0 D ;'. I actually don't mind that, a quick REPLACE() sorts it out.

GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24057 Visits: 9730
Yeah, the XML stuff definitely works best for strings of numbers. Add in text, and you run the risk of having to clean it up.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
RBarryYoung
RBarryYoung
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15150 Visits: 9518
Bruce W Cassidy (3/19/2009)
One word of warning: the FOR XML will also transform some control characters (such as a carriage return) into the form '& # x 0 D ;'.[/font]

That's know as the "Entitization problem" and there are several ways to address it. Using REPLACE alone is tough because there are a lot of different potential replacements.

The one that I use is to change this:
select
ColA,
stuff((select ', ' + ColB
from @TestTab t2
where t2.ColA = t1.ColA
for xml path('')),1,2,'')
from
@TestTab t1
group by
ColA;


into this:
select
ColA,
stuff( (select ', ' + ColB
from @TestTab t2
where t2.ColA = t1.ColA
for xml path(''), TYPE).value('.[1]', 'varchar(MAX)') ,1,2,'')
from
@TestTab t1
group by
ColA;



This fixes it, but adds some CPU overhead as well.


(edit: forgot the comma between PATH and TYPE...)

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Bruce W Cassidy
Bruce W Cassidy
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1317 Visits: 1033
Nice! Although you're missing a comma after the path('') :-D
RBarryYoung
RBarryYoung
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15150 Visits: 9518
Oops! Right you are...

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
StarNamer
StarNamer
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1672 Visits: 1992
Thanks, Barry.

Like Lynn, I'd used this trick but not really understood all the details of how it worked. Your explanation is very clear.

Derek
GSquared
GSquared
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24057 Visits: 9730
Nice enhancement there! Definitely going in my goodies library.

(Actually, before it came up in this thread, I hadn't ever seen this solution at all before. Had to figure it out from scratch to see what it was doing. It's a pretty slick trick.)

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
RBarryYoung
RBarryYoung
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15150 Visits: 9518
Thanks, Gus. I came up with that fix on my own. Adam Machanic, Ward Pond and some others have a different solution that they use (don't have it handy right now). It's more complicated, IMHO, but it might be faster (never compared them).

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Bruce Dow
Bruce Dow
Old Hand
Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)

Group: General Forum Members
Points: 366 Visits: 175
Concatenating Unique Field Values

Lynn's solution does a great job of concatenating field values into one string. However, it repeats duplicates, which can be quite annoying with many duplicates.

If we change the code to create the table and populate to:

declare @TestTab table (ColA int, ColB Varchar(10));

insert into @TestTab
select 1, 'John' union all
select 1, 'Bob' union all
select 1, 'Josh' union all
select 1, 'Bob' union all
select 1, 'Bob' union all
select 1, 'Bob' union all
select 2, 'Kathy' union all
select 2, 'Jill'
;


Then we get the results:

1 John, Bob, Josh, Bob, Bob, Bob
2 Kathy, Jill


If we change her solution to add the "distinct" keyword, we only get unique values.


select * from @TestTab;

select
ColA,
stuff((select distinct ', ' + ColB from @TestTab t2 where t2.ColA = t1.ColA for xml path('')),1,2,'')
from
@TestTab t1
group by
ColA;


Then we get the unique results:

1 Bob, John, Josh
2 Jill, Kathy


Thanks Lynn for a marvelous piece of code that I will use many times in the future.

Bruce
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16858 Visits: 19122
Wow, someone revived a 5 year old thread. w00t


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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search