Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 comma separated value from columns
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Posting Yak Master

230 Posts

Posted - 05/11/2013 :  03:58:33  Show Profile  Reply with Quote

SET @list = ''
SELECT @list = orgid + ' , ' + @list
FROM table1 
SELECT @list

Im trying to convert orgid column from table1 to output like 1234,1235,3345,3367,1238

However, the above code fetches me output as below :
1234 ,1235 ,3345 ,3367 ,1238

Can you please tell me how I can fix the space?

Aged Yak Warrior

549 Posts

Posted - 05/11/2013 :  11:02:58  Show Profile  Reply with Quote
Try this:

SET @list = ''
SELECT @list = RTRIM(LTRIM(orgid)) + ',' + @list
FROM table1 
SELECT @list

Go to Top of Page

Transact Charlie
Flowing Fount of Yak Knowledge

United Kingdom
3451 Posts

Posted - 05/12/2013 :  17:02:27  Show Profile  Visit Transact Charlie's Homepage  Reply with Quote
if you want the simple list of one column then it's probably better to just select the rows and then iterate over them in whatever application is calling the database -- then form the string there.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
Go to Top of Page
  Previous Topic Topic Next 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.03 seconds. Powered By: Snitz Forums 2000