SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Mutiple row value in one cell with comma seperated
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

reading2009
Starting Member

22 Posts

Posted - 01/08/2013 :  21:08:22  Show Profile  Reply with Quote
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 - 01/08/2013 :  21:22:28  Show Profile  Reply with Quote
any response please
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 01/08/2013 :  23:25:02  Show Profile  Reply with Quote

SELECT t.col1,
STUFF((SELECT ',' + col2 FROM table WHERE col1 = t.col1 FOR XML PATH('')),1,1,'')
FROM (SELECT DISTINCT col1 FROM table) t


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

Go to Top of Page

Jeff Moden
Aged Yak Warrior

USA
649 Posts

Posted - 01/09/2013 :  21:36:19  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000