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)
 SQL convert 1 column's values to 1 row string

Author  Topic 

ismailc
Constraint Violating Yak Guru

290 Posts

Posted - 2009-04-18 : 11:48:31
Hi, I need help please.

I have one column whith different values depending on result.
I would like to return a one row string result with a comma seperating the values.

The column output as follows:

Column A
--------
1
2
3

I would like to output as 1, 2, 3

Please Assist.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-18 : 15:30:15
[code]
DECLARE @Str varchar(8000)

SELECT @Str=COALESCE(@Str,'') + CAST(ColA as varchar(10)) + ','
FROM YourTable

SELECT @Str
[/code]
Go to Top of Page

ismailc
Constraint Violating Yak Guru

290 Posts

Posted - 2009-04-20 : 09:48:04
Thank You very much, exactly what i needed.
Searched long and hard.

How do i remove the "," at the end?
eg: Test1,Test2,Test3,

Regards
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-04-20 : 09:51:16
DECLARE @Str varchar(8000)

SELECT @Str=COALESCE(@Str,'') + CAST(ColA as varchar(10)) + ','
FROM YourTable

SELECT LEFT(@Str,LEN(@str)-1)
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-04-20 : 09:59:12

Try this too

DECLARE @Str varchar(8000)

SELECT @Str=COALESCE(@Str+',','') + CAST(ColA as varchar(10))
FROM YourTable

SELECT @Str



Madhivanan

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

ismailc
Constraint Violating Yak Guru

290 Posts

Posted - 2009-04-20 : 10:09:13
Great Suff - Thank You All :)
Go to Top of Page
   

- Advertisement -