Friday, February 29, 2008

Values of a column in comma separated format

This is a slightly tricky quey. Don get confused, just replace the column & table names and execute it.

DECLARE @str VARCHAR(5000)

SELECT
@str = COALESCE(@str+',','')+
(SELECT CAST(ColumnA as varchar(50)) FROM TableA A WHERE A.ColumnA = B.ColumnA) FROM
TableA B

Select @Str

Note: The tables used are the same tables with different aliases. And the whole query should be executed in a single batch(ie., no "Go" in between the these set of statements)

1 comment:

Anonymous said...

You write very well.