Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi SQL Experts,

I have table called Card it contains the 2 columns namely Cardnumber, PersonID


It contains the values as shown in the below:

Card Table:
CardNumber PersonID
1 200
2 200
3 300

Cardnumber column contains unique values.

I want the output as shown in the below:
PersonID ConcatedNatedCardnumbersByPersonIDWise

200 1 ,2
300 3

SQL
SELECT  PersonID, ConcatedNatedCardnumbersByPersonIDWise from card 



How can i achieve this.
Please help me. I will be waiting for valuable replies, i will appreciate you if you help me out with this issue.
Posted

Try this
SQL
select PersonID,STUFF(( SELECT '],[' + CardNumber
FROM         [Card Table]
Where [Card Table].PersonID = t2.PersonID
Order by CardNumber
FOR XML PATH('')), 1, 2, '') + ']' from [Card Table] as t2 group by PersonID
 
Share this answer
 
Comments
argeraju 31-Aug-14 6:38am    
thanks Vinay , you did great job...i appreciated ..
Vinay Mistry 31-Aug-14 6:45am    
Welcome
Try this
SQL
select PersonID, STUFF(
(select ','+ CardNumber from TempTable1 sch
where sch.PersonID = sch1.PersonID
FOR XML PATH(''))
,1,1,'') from TempTable1 sch1
group by PersonID
order by 1
 
Share this answer
 
Comments
argeraju 31-Aug-14 6:34am    
Great job...thanks for hepling i appreciated you.
I used Card instead of TempTable1 table in the above query , worked correctly...i appreciated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900