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 2005 Forums
 Transact-SQL (2005)
 concatenate all rows of a column

Author  Topic 

Babli
Yak Posting Veteran

53 Posts

Posted - 2007-09-19 : 00:33:11
Hi,
I have a requiremnet for a query in SQL 2005 where all the rows of a column needs to be concatenated.The number of rows may vary each time.ie,
If input is:
Col001
-------
ab
cd
ef
gh

Output should be like:
abcdefgh

I am trying to avoid cursors...

Please help...

Kristen
Test

22859 Posts

Posted - 2007-09-19 : 00:40:37
See: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=55210&SearchTerms=Combine%20values%20from%20rows%20into%20a%20column
Go to Top of Page

Babli
Yak Posting Veteran

53 Posts

Posted - 2007-09-19 : 01:03:59
Thanks guys...got the answer.Below is the query:

DECLARE @strValues varchar(8000)
SELECT @strValues = COALESCE(@strValues+',', '') + empname
FROM
(
SELECT DISTINCT Col001
FROM tab1
) X
ORDER BY Col001

SELECT [Result] = @strValues
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-09-19 : 04:07:04
As usual, If you want to show them in front end, do concatenation there

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-09-19 : 04:28:08
And since you are using SQL Server 2005,
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -