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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SELECT as Delimited String
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

12many
Starting Member

9 Posts

Posted - 03/14/2013 :  06:53:32  Show Profile  Reply with Quote
Hi There

I have a Table Groupmembers

GroupId | Member
1 5
1 6
1 7
2 8
2 9
2 10

I would like a result Like

Group | GroupMembers
1 5,6,7
2 8,9,10

I found this query on line and it does return the delimited members

DECLARE @list VARCHAR(MAX)
SELECT @list = COALESCE(@listStr+',' ,'') + CONVERT(VARCHAR(4),member)
FROM groupmembers
WHERE group = 1
SELECT @list

The problem here is that i can't then use this as a sub query.

Can any one Help

Many thanks

Ian

bandi
Flowing Fount of Yak Knowledge

India
2206 Posts

Posted - 03/14/2013 :  07:31:20  Show Profile  Reply with Quote
SELECT t1.GroupId, STUFF((SELECT ',' + CAST(s.Member AS VARCHAR) FROM @t s WHERE s.GroupId = t1.GroupId FOR XML PATH('')),1,1,'') AS CSV
FROM @t t1
GROUP BY t1.GroupId

--
Chandu
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