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
 Transact-SQL (2000)
 Concatenating Rows To A String

Author  Topic 

atulkukreja
Starting Member

13 Posts

Posted - 2004-09-01 : 09:16:16
I have a single column table with 10 rows.

I want to return a single string with the value in each row concatenated with a space or comma as a delimiter.

Example:

Select fName from Friend

fName
------
John Doe
Jim Johnson
Deepa Patel
----

Desired result:

John Doe, Jim Johnson, Deepa Patel, .....

Thanks.

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-09-01 : 09:25:12
[code]
Declare @list varchar(1000)

Declare @myTable table (fname varchar(100))
Insert Into @myTable
Select 'Corey Aldebol'
Union All Select 'Bill Gates'
Union All Select 'George Washington'
Union All Select 'Henry Ford'
Union All Select 'Heidi Klum'


Select @list = isnull(@list+',','') + fName From @myTable

Select @list
[/code]

Corey
Go to Top of Page

raymondpeacock
Constraint Violating Yak Guru

367 Posts

Posted - 2004-09-01 : 09:28:19
Or use COALESCE

Select @list = COALESCE(@list + ',' + fname, fname) From @myTable


Raymond
Go to Top of Page
   

- Advertisement -