Search code examples
sqlsql-servert-sqlstring-aggregation

Select multiple data from one column and update the combine result to another table


I have a table 'table A' as below and I want to select these data into one column and the result will update table B

+-----------+
| ID | Name |
+-----------+
| 1  |a     |
+-----------+
| 2  |b     |
+-----------+
| 3  |c     |
+-----------+

Result:

Name
-----
a b c

Solution

  • Use STRING_AGG()

    SELECT
        STRING_AGG(name,' ') Name
    FROM
        yourtable