Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
Simon writes "HiI'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 returned1. A2. B3. A4. A5. BHow can I get the string to just contain "A,B" rather than "A,B,A,A,B"Thanks"
Hi Simon, welcome to SQL Team!Something like this perhaps?
DECLARE @strValues varchar(8000)SELECT @strValues = COALESCE(@strValues+',', '') + MyColumnFROM( SELECT DISTINCT MyColumn FROM MyTable) XORDER BY MyColumnSELECT [Result] = @strValues
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2005-08-17 : 09:17:03
Kris, I like the way you welcome the new members The @strValues will have comma as the first characterSo you can remove it by SELECT [Result] = substring(@strValues,2,len(@strValues))MadhivananFailing to plan is Planning to fail