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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Put Values of Column Together In 1 String. Help

Author  Topic 

besadmin
Posting Yak Master

116 Posts

Posted - 2012-06-07 : 16:15:18
Hey Friends, hopefully this isn't too silly of a question. We are trying to take a column of values and put them together in a string.
So the Table might look like this:

Column 1

AAA

BBB

CCC

DDD



So we want a query that can give us a result from that table of:

'AAA, BBB, CCC, DDD'

All together in 1 string separated by a comma or something.

Thanks a ton for any replys! Much Appreciated.

- S

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-06-07 : 19:16:52
[code]SELECT STUFF((
SELECT
','+Column1 AS [text()]
FROM
YourTable
FOR XML PATH('')),1,1,'');[/code]
Go to Top of Page

besadmin
Posting Yak Master

116 Posts

Posted - 2012-06-08 : 11:00:33
sunitabeck, thanks a ton for the reply. It worked flawlessly.

I have never seen the STUFF keyword, pretty cool.

We also got it working using COLESCE:

DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr+', ' ,'') + [File_Name]
FROM TableName
SELECT @listStr


Just to let everyone know another way too i suppose.

Thanks again for your response. Perfect! and greatly appreciated.

Later Friends!

- S
Go to Top of Page
   

- Advertisement -