简体   繁体   中英

SQL Server : multiple rows to one column

I have following table:

 TXT_Nr |   TXTL_LineNr    |    TXTL_String |   TXTL_Lgt|
 -------|------------------|----------------|-----------|
 10001  |   1              |    hello,      |    6      |
 10001  |   2              |    my name     |    7      |
 10001  |   N-lines can be |     ...        |    ...    |

Next result required:

TXT_Nr  | TXTL_String 
--------|---------------
10001   | hello, my name

Using STUFF and GROUP BY method to concatenate string.

SELECT TXT_Nr , STUFF( (SELECT ',' + TXTL_String FROM #Table Inr WHERE 
Inr.TXT_Nr = otr.TXT_Nr FOR XML PATH('')),1,1,'')
FROM #Table Otr
GROUP BY TXT_Nr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM