Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Concatenate string using Group By

Posted on 2007-11-22
4
Medium Priority
?
9,177 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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

972 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