get a comma delimited string from rows [duplicate]









up vote
1
down vote

favorite













This question already has an answer here:



  • How to concatenate text from multiple rows into a single text string in SQL server?

    44 answers



What will be the query in MS Sql Server to concatenate my rows in one string , delimited by comma (Like shown below)



query to delimeter string










share|improve this question















marked as duplicate by krokodilko, Shnugo sql-server
Users with the  sql-server badge can single-handedly close sql-server questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 22 '17 at 10:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
    – JSapkota
    Jan 22 '17 at 6:06










  • SQL SERVER, i have no idea how to do.....
    – Ajit Kumar
    Jan 22 '17 at 6:33






  • 1




    Please have a look here stackoverflow.com/q/194852/1530987
    – bhageena
    Jan 22 '17 at 6:46










  • Please read How to Ask.
    – Zohar Peled
    Jan 22 '17 at 8:07














up vote
1
down vote

favorite













This question already has an answer here:



  • How to concatenate text from multiple rows into a single text string in SQL server?

    44 answers



What will be the query in MS Sql Server to concatenate my rows in one string , delimited by comma (Like shown below)



query to delimeter string










share|improve this question















marked as duplicate by krokodilko, Shnugo sql-server
Users with the  sql-server badge can single-handedly close sql-server questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 22 '17 at 10:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
    – JSapkota
    Jan 22 '17 at 6:06










  • SQL SERVER, i have no idea how to do.....
    – Ajit Kumar
    Jan 22 '17 at 6:33






  • 1




    Please have a look here stackoverflow.com/q/194852/1530987
    – bhageena
    Jan 22 '17 at 6:46










  • Please read How to Ask.
    – Zohar Peled
    Jan 22 '17 at 8:07












up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:



  • How to concatenate text from multiple rows into a single text string in SQL server?

    44 answers



What will be the query in MS Sql Server to concatenate my rows in one string , delimited by comma (Like shown below)



query to delimeter string










share|improve this question
















This question already has an answer here:



  • How to concatenate text from multiple rows into a single text string in SQL server?

    44 answers



What will be the query in MS Sql Server to concatenate my rows in one string , delimited by comma (Like shown below)



query to delimeter string





This question already has an answer here:



  • How to concatenate text from multiple rows into a single text string in SQL server?

    44 answers







sql sql-server sql-server-2012






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 '17 at 8:58









Hadi

16.3k62570




16.3k62570










asked Jan 22 '17 at 5:58









Ajit Kumar

1513




1513




marked as duplicate by krokodilko, Shnugo sql-server
Users with the  sql-server badge can single-handedly close sql-server questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 22 '17 at 10:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by krokodilko, Shnugo sql-server
Users with the  sql-server badge can single-handedly close sql-server questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Jan 22 '17 at 10:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
    – JSapkota
    Jan 22 '17 at 6:06










  • SQL SERVER, i have no idea how to do.....
    – Ajit Kumar
    Jan 22 '17 at 6:33






  • 1




    Please have a look here stackoverflow.com/q/194852/1530987
    – bhageena
    Jan 22 '17 at 6:46










  • Please read How to Ask.
    – Zohar Peled
    Jan 22 '17 at 8:07
















  • Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
    – JSapkota
    Jan 22 '17 at 6:06










  • SQL SERVER, i have no idea how to do.....
    – Ajit Kumar
    Jan 22 '17 at 6:33






  • 1




    Please have a look here stackoverflow.com/q/194852/1530987
    – bhageena
    Jan 22 '17 at 6:46










  • Please read How to Ask.
    – Zohar Peled
    Jan 22 '17 at 8:07















Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
– JSapkota
Jan 22 '17 at 6:06




Which DBMS are you using? Oracle or SQL Server? And Have you tried something?
– JSapkota
Jan 22 '17 at 6:06












SQL SERVER, i have no idea how to do.....
– Ajit Kumar
Jan 22 '17 at 6:33




SQL SERVER, i have no idea how to do.....
– Ajit Kumar
Jan 22 '17 at 6:33




1




1




Please have a look here stackoverflow.com/q/194852/1530987
– bhageena
Jan 22 '17 at 6:46




Please have a look here stackoverflow.com/q/194852/1530987
– bhageena
Jan 22 '17 at 6:46












Please read How to Ask.
– Zohar Peled
Jan 22 '17 at 8:07




Please read How to Ask.
– Zohar Peled
Jan 22 '17 at 8:07












2 Answers
2






active

oldest

votes

















up vote
8
down vote













Use STUFF and FOR XML:



Create and populate sample table (Please save us this step in your future questions)



DECLARE @T AS TABLE
(
Name varchar(10)
)

INSERT INTO @T VALUES
('John'),
('Vicky'),
('Sham'),
('Anjli'),
('Manish')


The query:



SELECT STUFF((
SELECT ',' + Name
FROM @T
FOR XML PATH('')
), 1, 1, '') As [output];


Results:



output
John,Vicky,Sham,Anjli,Manish





share|improve this answer





























    up vote
    2
    down vote













    Assuming That your column name is NAME And table name is MYTABLE you can use the Following query:



    DECLARE @strTemp VARCHAR(MAX)

    SET @strTemp = ''

    SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
    FROM MYTABLE

    --Remove last comma
    SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)

    --Get Result
    SELECT @strTemp


    You can filter null records using the following



    SELECT @strTemp = @strTemp + NAME + ','
    FROM MYTABLE
    WHERE NAME IS NOT NULL





    share|improve this answer





























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      8
      down vote













      Use STUFF and FOR XML:



      Create and populate sample table (Please save us this step in your future questions)



      DECLARE @T AS TABLE
      (
      Name varchar(10)
      )

      INSERT INTO @T VALUES
      ('John'),
      ('Vicky'),
      ('Sham'),
      ('Anjli'),
      ('Manish')


      The query:



      SELECT STUFF((
      SELECT ',' + Name
      FROM @T
      FOR XML PATH('')
      ), 1, 1, '') As [output];


      Results:



      output
      John,Vicky,Sham,Anjli,Manish





      share|improve this answer


























        up vote
        8
        down vote













        Use STUFF and FOR XML:



        Create and populate sample table (Please save us this step in your future questions)



        DECLARE @T AS TABLE
        (
        Name varchar(10)
        )

        INSERT INTO @T VALUES
        ('John'),
        ('Vicky'),
        ('Sham'),
        ('Anjli'),
        ('Manish')


        The query:



        SELECT STUFF((
        SELECT ',' + Name
        FROM @T
        FOR XML PATH('')
        ), 1, 1, '') As [output];


        Results:



        output
        John,Vicky,Sham,Anjli,Manish





        share|improve this answer
























          up vote
          8
          down vote










          up vote
          8
          down vote









          Use STUFF and FOR XML:



          Create and populate sample table (Please save us this step in your future questions)



          DECLARE @T AS TABLE
          (
          Name varchar(10)
          )

          INSERT INTO @T VALUES
          ('John'),
          ('Vicky'),
          ('Sham'),
          ('Anjli'),
          ('Manish')


          The query:



          SELECT STUFF((
          SELECT ',' + Name
          FROM @T
          FOR XML PATH('')
          ), 1, 1, '') As [output];


          Results:



          output
          John,Vicky,Sham,Anjli,Manish





          share|improve this answer














          Use STUFF and FOR XML:



          Create and populate sample table (Please save us this step in your future questions)



          DECLARE @T AS TABLE
          (
          Name varchar(10)
          )

          INSERT INTO @T VALUES
          ('John'),
          ('Vicky'),
          ('Sham'),
          ('Anjli'),
          ('Manish')


          The query:



          SELECT STUFF((
          SELECT ',' + Name
          FROM @T
          FOR XML PATH('')
          ), 1, 1, '') As [output];


          Results:



          output
          John,Vicky,Sham,Anjli,Manish






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 22 '17 at 8:03

























          answered Jan 22 '17 at 7:55









          Zohar Peled

          51.5k73172




          51.5k73172






















              up vote
              2
              down vote













              Assuming That your column name is NAME And table name is MYTABLE you can use the Following query:



              DECLARE @strTemp VARCHAR(MAX)

              SET @strTemp = ''

              SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
              FROM MYTABLE

              --Remove last comma
              SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)

              --Get Result
              SELECT @strTemp


              You can filter null records using the following



              SELECT @strTemp = @strTemp + NAME + ','
              FROM MYTABLE
              WHERE NAME IS NOT NULL





              share|improve this answer


























                up vote
                2
                down vote













                Assuming That your column name is NAME And table name is MYTABLE you can use the Following query:



                DECLARE @strTemp VARCHAR(MAX)

                SET @strTemp = ''

                SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
                FROM MYTABLE

                --Remove last comma
                SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)

                --Get Result
                SELECT @strTemp


                You can filter null records using the following



                SELECT @strTemp = @strTemp + NAME + ','
                FROM MYTABLE
                WHERE NAME IS NOT NULL





                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Assuming That your column name is NAME And table name is MYTABLE you can use the Following query:



                  DECLARE @strTemp VARCHAR(MAX)

                  SET @strTemp = ''

                  SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
                  FROM MYTABLE

                  --Remove last comma
                  SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)

                  --Get Result
                  SELECT @strTemp


                  You can filter null records using the following



                  SELECT @strTemp = @strTemp + NAME + ','
                  FROM MYTABLE
                  WHERE NAME IS NOT NULL





                  share|improve this answer














                  Assuming That your column name is NAME And table name is MYTABLE you can use the Following query:



                  DECLARE @strTemp VARCHAR(MAX)

                  SET @strTemp = ''

                  SELECT @strTemp = @strTemp + ISNULL(NAME,'') + ','
                  FROM MYTABLE

                  --Remove last comma
                  SET @strTemp = SUBSTRING(@strTemp ,1,LEN(@strTemp ) -1)

                  --Get Result
                  SELECT @strTemp


                  You can filter null records using the following



                  SELECT @strTemp = @strTemp + NAME + ','
                  FROM MYTABLE
                  WHERE NAME IS NOT NULL






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jan 22 '17 at 9:07

























                  answered Jan 22 '17 at 8:54









                  Hadi

                  16.3k62570




                  16.3k62570













                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto