Solved

Concatenate string using Group By

Posted on 2007-11-22
4
9,136 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
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
AngelllI - v.v.v. cool solution....
0
 
LVL 1

Expert Comment

by:Computer101
Comment Utility
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now