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 2000 Forums
 SQL Server Development (2000)
 concatenate 1 column 'n' rows into one string

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-08-31 : 10:30:31
Robert writes "I have a select query that returns 1 column and n rows, I want to concatenate all the values into one comma separated string.

It's be nice to do this in a MS-SQL Stored procedure.
I'm presently using SQL-2005"

nr
SQLTeam MVY

12543 Posts

Posted - 2006-08-31 : 10:35:37
declare @s varchar(8000)
select @s = coalesce(@s + ',','') + col
from tbl
select @s

replace tbl with your query

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-31 : 10:35:52
[code]
declare @csv varchar(8000)

select @csv = isnull(@csv, '') + col + ','
from table
[/code]


KH

Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-08-31 : 10:56:18
quote:
Originally posted by khtan


declare @csv varchar(8000)

select @csv = isnull(@csv, '') + col + ','
from table



KH





That'll leave a trailing comma - have a look at my post.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -