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