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
 General SQL Server Forums
 New to SQL Server Programming
 comma separated value from columns

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2013-05-11 : 03:58:33
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

549 Posts

Posted - 2013-05-11 : 11:02:58
Try this:
[CODE]

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

[/CODE]
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2013-05-12 : 17:02:27
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
   

- Advertisement -