how to build a Comma seperated list from a table in SQL CE?

82 views Asked by At

how to build a Comma seperated list from a table in SQL CE ?

I have table named Group and it has two columns ID and Name

I want to select a one comma seperated string from Group table.

So IF I have 3 records as follows in group table

ID  | Name 
 1  | Msh
 2  | Nsh
 3  | Lsh 

I want to get a one comma seperated list of all three names like this Msh,Nsh,Lsh

How can I get this done is SQL CE ?

2

There are 2 answers

1
Sunil Naudiyal On

Try this..

DECLARE @COMMA VARCHAR(MAX)
 SET @COMMA =''
 SELECT @COMMA =@COMMA +name+',' FROM yourtablename
 SELECT SUBSTRING(@COMMA,0,LEN(@COMMA))
1
Avrajit On
    You can develop a simple logic in SQL. This is a dummy code you can try and modify the code as per your requirements.

    declare
        i varchar2(100);
        j varchar2(100);
        begin
        for i in (select name from avrajit)
        loop
        j:=i.name||','||j;
        end loop;
        dbms_output.put_line(j);
        end;

---------------------------------------
OUTPUT
---------------------------------------

Hitesh,Sushil2,Mukul,Shyam,Nikheel,Avrajit,Sushil,

Statement processed.