Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Mutiple row value in one cell with comma seperated

Author  Topic 

reading2009
Starting Member

22 Posts

Posted - 2013-01-08 : 21:08:22
my data:
col1 col2
name1 abc
name1 def
name1 xyz
name2 abc
name2 ghy
name3 xyz
name3 hyu

desired output
col1 col2
name1 abc,def,xyz
name2 abc,ghy
name3 xyz,hyu

how can i get my desired output through a sql query?
thank you.

reading2009
Starting Member

22 Posts

Posted - 2013-01-08 : 21:22:28
any response please
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-08 : 23:25:02
[code]
SELECT t.col1,
STUFF((SELECT ',' + col2 FROM table WHERE col1 = t.col1 FOR XML PATH('')),1,1,'')
FROM (SELECT DISTINCT col1 FROM table) t
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Jeff Moden
Aged Yak Warrior

652 Posts

Posted - 2013-01-09 : 21:36:19
quote:
Originally posted by reading2009

any response please



Lordy you're impatient. This is a forum... not your personal help button.

--Jeff Moden
RBAR is pronounced "ree-bar" and is a "Modenism" for "Row By Agonizing Row".

First step towards the paradigm shift of writing Set Based code:
"Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

When writing schedules, keep the following in mind:
"If you want it real bad, that's the way you'll likely get it."
Go to Top of Page
   

- Advertisement -