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 2000 Forums
 SQL Server Development (2000)
 Coalesce combine rows into comma delimited string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AskSQLTeam
Ask SQLTeam Question

USA
0 Posts

Posted - 08/17/2005 :  08:16:41  Show Profile  Visit AskSQLTeam's Homepage  Reply with Quote
Simon writes "Hi

I've read article ID 2368 which illustrates the coalesce method of combining rows to a comma delimted string which is great but do you know any way for the results in the string to be distinct.

Say my query returned

1. A
2. B
3. A
4. A
5. B

How can I get the string to just contain "A,B" rather than "A,B,A,A,B"

Thanks"

madhivanan
Premature Yak Congratulator

India
22713 Posts

Posted - 08/17/2005 :  08:20:03  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Refer this
http://sqljunkies.com/WebLog/amachanic/archive/2004/11/10/5065.aspx?Pending=true

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 08/17/2005 :  08:33:12  Show Profile  Reply with Quote
Hi Simon, welcome to SQL Team!

Something like this perhaps?

DECLARE @strValues varchar(8000)
SELECT @strValues = COALESCE(@strValues+',', '') + MyColumn
FROM
(
    SELECT DISTINCT MyColumn
    FROM MyTable
) X
ORDER BY MyColumn

SELECT [Result] = @strValues
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22713 Posts

Posted - 08/17/2005 :  09:17:03  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Kris, I like the way you welcome the new members

The @strValues will have comma as the first character
So you can remove it by

SELECT [Result] = substring(@strValues,2,len(@strValues))




Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 12/16/2005 :  16:02:56  Show Profile  Reply with Quote
You forgot to use a cursor

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam
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