Saturday, February 25, 2012

Reducing 5 values to 1 value

I am trying to take values that exist in a single column and create a column
elsewhere that contains comma separated values of that dataset.
The '5' is arbitrary...
For example,
Greg
John
Bill
Carol
Judy
Julie
Would look like
Greg, John, Bill, Carol, Judy, Julie
Appreciate in advance any thoughts....
GregA way, maybe not the best way
DECLARE @.name varchar(512)
DECLARE @.list varchar(512)
SET @.name = ''
SET @.list = ''
DECLARE names_cursor STATIC FORWARD_ONLY
FOR
SELECT DISTINCT [name] FROM [YourTable]
OPEN names_cursor
FETCH NEXT FROM names_cursor INTO @.name
WHILE (@.@.FETCH_STATUS = 0)
BEGIN
SET @.list = @.list + @.name
FETCH NEXT FROM names_cursor INTO @.name
END
INSERT INTO [OtherTable] VALUES (@.list)
CLOSE names_cursor
DEALLOCATE names_cursor
Look at
http://www.microsoft.com/technet/co...r />
931d-e02a
cbb88505
for a trigger that I wrote that would keep those two tables syncrhonized.
"Greg" wrote:

> I am trying to take values that exist in a single column and create a colu
mn
> elsewhere that contains comma separated values of that dataset.
> The '5' is arbitrary...
> For example,
> Greg
> John
> Bill
> Carol
> Judy
> Julie
> Would look like
> Greg, John, Bill, Carol, Judy, Julie
> Appreciate in advance any thoughts....
> Greg
>|||Ahh I asked this one the other day...
See http://www.aspfaq.com/show.asp?id=2529
Or see this post..
http://groups.google.co.uk/group/mi...7f3f3d7de2dd4fa
Barry

No comments:

Post a Comment