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)
 Put Values of Column Together In 1 String. Help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

besadmin
Posting Yak Master

116 Posts

Posted - 06/07/2012 :  16:15:18  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

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

besadmin
Posting Yak Master

116 Posts

Posted - 06/08/2012 :  11:00:33  Show Profile  Reply with Quote
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
  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.08 seconds. Powered By: Snitz Forums 2000