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
 General SQL Server Forums
 New to SQL Server Programming
 comma separated value from columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

learning_grsql
Posting Yak Master

230 Posts

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


DECLARE @list VARCHAR(MAX)
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?

MuMu88
Aged Yak Warrior

547 Posts

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


DECLARE @list VARCHAR(MAX)
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.
http://nosqlsolution.blogspot.co.uk/
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.06 seconds. Powered By: Snitz Forums 2000