This question already has answers here:
Turning a Comma Separated string into individual rows (16 answers)
Alternative of string_split Function on lower compatibility level (2 answers)
How to concatenate text from multiple rows into a single text string in SQL Server (47 answers)
Closed 4 days ago.
TableDO
| DOID | TranID |
| -------- | ------ |
| 1 | 1 2 3 |
| 2 | 2 4 |
TblTransporter
| TranID |Transporter |
| -------- | -------- |
| 1 | ABC Tran |
| 2 | BBC Tran |
| 3 | CBC Tran |
| 4 | DBC Tran |
Result require From TableDO
| DOID | Transporter |
| -------- | -------- |
| 1 | ABC Tran,BBC Tran,CBC Tran|
| 2 | BBC Tran,DBC Tran |
I have tried
Select o.DoNo,t.Transporter as tpt
From DO o
outer apply String_Split(o.Transporter,' ') s
left join Transporter as t on t.TID = s.value
Which Shows Result
| DONO | Tpt |
| -------- | -------- |
| 1 | ABC Tran |
| 1 | BBC Tran |
| 1 | CBC Tran |
| 2 | BBC Tran |
| 2 | DBC Tran |
I do not want to use String_Split Function as it requre Database Compatibility.
3条答案
按热度按时间z2acfund1#
Here's a solution for all those who still think SQL Server 2016 is the latest and greatest version:
It looks a bit busy but what it does is create an xml from the list of tranIds, and then split it by using nodes, and then concatenates it back using another FOR XML PATH
cnh2zyt32#
You can try with STRING_AGG()
ygya80vv3#
This is an other solution using
GROUP BY
andSTRING_AGG
only :Result :
Demo here