sql server 将逗号分隔的行转多列并且带附加ID

原表结构:

 

转换后的表结构:

 

 sql:

SELECT TU.Id as UserId,SUBSTRING(TU.WorkingForGroupIds, number ,CHARINDEX(',',TU.WorkingForGroupIds+',',number)-number) as WrokGroupId 
FROM Team_Users AS TU,master..spt_values s
WHERE s.number >=1
AND s.type = 'P'
AND SUBSTRING(','+TU.WorkingForGroupIds,s.number,1) = ',' and TU.WorkingForGroupIds<>'' AND TU.WorkingForGroupIds IS NOT NULL

 

posted on 2020-09-08 11:32  哦呵呵G  阅读(437)  评论(0编辑  收藏  举报

导航