34,873
社区成员
发帖
与我相关
我的任务
分享select 申请编号,
人员编号=ISNULL(人员编号,name),
人员名称=case when 人员编号 IS null then '' else name end
from (
select 申请编号,
SUBSTRING(a.人员名称,number,CHARINDEX(',',a.人员名称+',',number)-number) as name
from a join master..spt_values s
on s.number between 1 and LEN(a.人员名称)
where type='p' and SUBSTRING(','+a.人员名称,number,1)=',') k left join b on k.name=b.名称
if OBJECT_ID('a') is not null
drop table a
go
create table a (申请编号 varchar(10) , 人员名称 varchar(100))
insert a select
'00001' ,'张三,李四,王五'
go
if OBJECT_ID('b') is not null
drop table b
go
create table b (人员编号 varchar(10) , 名称 varchar(10))
insert b select
'01', '张三' union select
'02', '李四'
go
select 申请编号,
人员编号=ISNULL(人员编号,name),
人员名称=ISNULL(人员编号,'')
from (
select 申请编号,
SUBSTRING(a.人员名称,number,CHARINDEX(',',a.人员名称+',',number)-number) as name
from a join master..spt_values s
on s.number between 1 and LEN(a.人员名称)
where type='p' and SUBSTRING(','+a.人员名称,number,1)=',') k left join b on k.name=b.名称
/*
申请编号 人员编号 人员名称
---------- ---------- ----------
00001 01 01
00001 02 02
00001 王五 */