|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 9:15 AM
Points: 1,304,
Visits: 7,125
|
|
Glad most folks liked it. Here is the actual code (you'll have to remove the *'s around amp to make it work):
WITH cteData (ID, Colors) AS (SELECT 1, 'Blue' UNION SELECT 2, 'Red' UNION SELECT 3, 'Yellow' UNION SELECT 4, 'Gray' UNION SELECT 1, 'Purple' UNION SELECT 2, 'Orange' UNION SELECT 3, 'Green' UNION SELECT 4, 'Black & White')
SELECT REPLACE(STUFF((SELECT ', ' + d.Colors FROM [cteData] d WHERE c.[ID] = d.[ID] ORDER BY [d].[Colors] FOR XML PATH('')),1,1,''), '&*amp*;', '&') [ColorList] FROM [cteData] c GROUP BY c.[ID] ORDER BY c.[ID]
bc
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Friday, March 15, 2013 2:43 PM
Points: 3,924,
Visits: 1,554
|
|
Good question. The best part was image instead of text. By looking at the question I thought /* The replace function requires 3 argument(s) */ would be the answer but while writing the same code in SSMS I realized it would return four rows and option 1 should be the answer.
SQL DBA.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 7:03 AM
Points: 3,572,
Visits: 935
|
|
| Good question. (Maybe because I got it right?). No, actually it was a good question that made me read slowly and think it through.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Today @ 10:31 AM
Points: 18,857,
Visits: 12,442
|
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 3:03 AM
Points: 2,798,
Visits: 197
|
|
Good One. I also used STUFF and FOR XML to solve a problem at my workplace, but was surprised to see &
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 7,185,
Visits: 7,285
|
|
A good question.
It took me a while to work out because it relies on the fact that ','+d.Colors doesn't have a column name so that for xml path just concatenates the two comma-prefixed colors rather than generating a subelement for each, and I have never used that feature (I've always wanted subelements on the rare occassions I've used for xml) so it didn't click quickly.
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
http://es.linkedin.com/in/tomthomsonsoftware
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 8:14 AM
Points: 154,
Visits: 380
|
|
Good Question
I got it right but for the wrong reason's(process of elimination) in my opinion.
I must say I am not a fan of the image since I could not copy the SQL and format it(not run it) to my liking.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Thursday, June 13, 2013 8:50 PM
Points: 3,208,
Visits: 4,178
|
|
bc_ (2/19/2010) Here is the actual code It is very interesting to see a proportional font (looks like Arial) on the screenshot Do you really use that font for everyday coding instead of a fixed-width font (e.g. Courier New)?
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, June 10, 2013 9:15 AM
Points: 1,304,
Visits: 7,125
|
|
ARIAL BLACK
is there an advantage to using a fixed width font? or is it personal preference?
bc
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Tuesday, May 28, 2013 9:52 AM
Points: 814,
Visits: 1,137
|
|
| I would say fixed width makes it easier to indent your code since every character fits in a same size block. For me, that's the only reason, it's mere personal preference beyond that. Not sure if others have other reasons why?
|
|
|
|