SQL - 解析字符串
ID Names 1 Aaron, Betsy, Cindy
2 Dillon, Eric, Fred
我想通过名称列解析并使其返回:
ID Names 1 Aaraon
1 Betsy
1 Cindy
2 Dillon
我已经发现了几个功能,网上说解析名称列,但不会将ID绑定回它。
回答:
怎么是这样的:
;with cte (id, name, names) as (
select id,
cast(left(names, charindex(',',names+',')-1) as varchar(50)) name,
stuff(names, 1, charindex(',',names+','), '') names
from yourtable
union all
select id,
cast(left(names, charindex(',',names+',')-1) as varchar(50)) name,
stuff(names, 1, charindex(',',names+','), '') names
from cte
where names > ''
)
select id, name
from cte
order by id
见SQL Fiddle with Demo
返回结果:
| ID | NAME | ---------------
| 1 | Aaron |
| 1 | Betsy |
| 1 | Cindy |
| 2 | Dillon |
| 2 | Eric |
| 2 | Fred |
以上是 SQL - 解析字符串 的全部内容, 来源链接: utcz.com/qa/261466.html