Solved

concatenate column from a query

Posted on 2011-03-02
3
197 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
  • 2
3 Comments
 
LVL 32

Expert Comment

by:ewangoya
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:
ewangoya 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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