locked
get column data in comma RRS feed

  • Question

  • User-807418713 posted

    Hello

    I have one filed Which has following Data

    Field1

    A001

    K03

    M1

    N1

    I want result set 

    'A001','K03','M1','N1'

    How to get like this data i have  more then 2000 records

    thanking you

    Tuesday, July 14, 2020 7:09 AM

Answers

  • User-1330468790 posted

    Hi Gopi.MCA,

     

    Here is a workaround with SQL query statement.

    I have below data in the database: 

    The sql statement is :

    Declare @Numbers AS Nvarchar(MAX) 
    SELECT  @Numbers = COALESCE(@Numbers + ',', '') + '''' +  [name] + ''''
    FROM   [city_table] 
    SELECT @Numbers

    Result:

     

    I understand that there are some methods like using XML and STRING_AGG. However, XML is not easier to understand and use. STRING_AGG requires SQL SERVER 2017 or later.

    If you are interested in above two methods, you could refer to here: How to concatenate text from multiple rows into a single text string in SQL server?

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2020 8:29 AM