Solved

concatenate column from a query

Posted on 2011-03-02
3
200 Views
Last Modified: 2012-05-11
I have the following query result

SELECT
Number,
Code
FROM #tmp

With the following result
Number      Code
11      003XX  
11      001XX  
17      003XX  

But I want to concatennate the result to look like

Number      Code
11      003XX , 001XX  
17      003XX  
0
Comment
Question by:ed_j
[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
  • 2
3 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35022016
try
SELECT Number,
	  STUFF(
		(
		SELECT
		  ' ' + Code
		FROM #tmp
		WHERE Number = A.Number
		FOR XML PATH('')
		), 1, 1, ''
	  ) As Code
from #temp A
group by Number

Open in new window

0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 35022026
You wanted a comma separated list so change it to
SELECT Number,
	  STUFF(
		(
		SELECT ', ' + Code
		FROM #tmp
		WHERE Number = A.Number
		FOR XML PATH('')
		), 1, 1, ''
	  ) As Code
from #temp A
group by Number

Open in new window

0
 
LVL 26

Expert Comment

by:tigin44
ID: 35022054
SELECT B.Number, STUFF(       
      (SELECT  ',' + A.Code
      FROM #tmp A
      WHERE A.Number = B.Number
      FOR XML PATH ('')), 1,1,'')      
FROM AAA B
GROUP BY B.Number      
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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