Solved

concatenate column from a query

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
spx for moving values to new table 5 75
SQL Query Syntax:  Why is the CTE Pulling in More Data Than Asked For? 5 73
SQL query with cast 38 59
How can I find this data? 3 25
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 …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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