Solved

Concatenate string using Group By

Posted on 2007-11-22
4
9,165 Views
Last Modified: 2008-02-21
How do I concatanate strings with MSSQL?

e.g. table values:
cGroup  cText
A           L
A           M
B           V

query:
select cGroup, concat(cText) as cTextConcat
from myTable
group by cGroup

Result set:
cGroup cTextConcat
A         LM
B         V
0
Comment
Question by:therealdreamer81
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20334964
you cannot do it without using a UDF (function)....

create function dbo.ConcatText ( @Group varchar(10))
returns varchar(100)
as
begin
  declare @res
  set @res = ''
  select @res = @res + cText From myTable where cGroup = @group order by cText
  return @res
end

and use it like this:

select cGroup, dbo.ConcatText(cGroup) cTextConcat
from myTable
group by cGroup
0
 
LVL 6

Expert Comment

by:PaultheBroker
ID: 20335201
AngelllI - v.v.v. cool solution....
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20953227
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question