原数据:
拆分后数据:
select 'IP'=substring(a.UserName,b.number,charindex(',',a.UserName+',',b.number)-b.number) from TestRows2Columns a inner join master.dbo.spt_values b on b.number between 1 and len(a.UserName) and substring(','+a.UserName,b.number,1)=',' where b.type='P' and Id=1
语句总结套用:
select 这个地方就是查询的字段,然后IP是自定义的名字 'IP'=substring(拆分的字段名,b.number,charindex('拆分的符号',拆分的字段名+',',b.number)-b.number) from 你的表名 a inner join master.dbo.spt_values b on b.number between 1 and len(拆分的字段名) and substring(','+拆分的字段名,b.number,1)='拆分的符号' where b.type='P' 后边可以是条件 and Id=1