Re: SELECT records to commaseparated string??

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: avnrao (avn_at_newsgroups.com)
Date: 11/03/04


Date: Wed, 3 Nov 2004 14:12:50 +0530

not sure if you know this..

Declare @ConcatString nvarchar(4000)
SET @ConcatString = ''
Select @ConcatString = @ConcatString +
  (CASE WHEN @ConcatString = '' THEN
   Convert(Varchar(10),ID)
   ELSE
   ',' + Convert(Varchar(10),ID)
  END)
 From Table
print @ConcatString

Av.
http://avdotnet.rediffblogs.com
http://www28.brinkster.com/avdotnet

"Kjell Brandes" <kjell@brandes.tv> wrote in message
news:uS8lD0XwEHA.1452@TK2MSFTNGP11.phx.gbl...
> Is there a simple way to do a "select columnname from tablename" which
> returns 10 rows getting this rows into a commaseparated string.
>
> ex.
> above select would return:
> columnname
> 1
> 2
> 3
> 4
> 5
> 6
> .
> .
> .
> .
> and so on.....
>
> I would like it in this form
> 1, 2, 3, 4,.........
>
> I know UDFs or PROCs is a way I just wonder if there might be a simplier
> solution to this.
>
> Thanx
> Kjell Brandes
>
>



Relevant Pages