Solved

Concatenate string using Group By

Posted on 2007-11-22
4
9,152 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Freeze portion of datamart 2 21
SQL to JSON 14 31
Problem with MySQL query - graph 3 23
T-SQL: I need to add an index on a field 5 16
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

735 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