Solved

concatenate column from a query

Posted on 2011-03-02
3
195 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now