例如:比如 产品字段protest有逗号组合,现在把这些分割开来。

变成如下:

方法如下:

create table dbo.test_name(
id int null
,name varchar(10) null
,protest varchar(500) null 
);


insert into dbo.test_name values ('1','张三','电脑,电视机,扫地机');
insert into dbo.test_name values ('2','李四','扫地机');
insert into dbo.test_name values ('3','王五','');
insert into dbo.test_name values ('4','宋小','笔记本电脑,手机');



select a.id
      ,a.name
      ,a.protest
	  ,SUBSTRING(a.protest,number,CHARINDEX(',',a.protest+',',number)-number) as protestxx
  from test_name a  with(nolock) ,master..spt_values  with(nolock) 
  where type='p'
   and SUBSTRING(','+a.protest,number,1)=','

如果查询出剔除空的数据如下:

select a.id
      ,a.name
      ,a.protest
	  ,SUBSTRING(a.protest,number,CHARINDEX(',',a.protest+',',number)-number) as protestxx
  from test_name a  with(nolock) ,master..spt_values  with(nolock) 
  where number >= 1 and number < len(a.protest)
   and type='p'
   and SUBSTRING(','+a.protest,number,1)=','

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐