SQL SERVER 把逗号隔开的字符串拆分成行,有效哦!
例如:比如 产品字段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
·
例如:比如 产品字段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)=','
更多推荐
已为社区贡献1条内容
所有评论(0)