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
Solved

concatenate column from a query

Posted on 2011-03-02
3
198 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
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.

839 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