------解决方案-------------------- charindex(' ',[CODE2]+' ',b.P2)+1 from test a join T b on a.id=b.id where charindex(' ',[CODE2]+' ',b.P2)>0 ) select a.id, a.CODE1, name=substring(a.[CODE2]+' ',b.P1,b.P2 - b.P1 - 1) from test a join T b on a.id=b.id order by 1 /* id CODE1 name -------------------------- 1 AA AA 1 AA BB 1 AA CC 2 BB FF 2 BB EE 2 BB DD */
--> 测试数据:[A1] if object_id('[A1]') is not null drop table [A1] create table [A1]( [编码] varchar(2), [内容] varchar(1) ) insert [A1] select '01','a' union all select '02','b' union all select '03','c' union all select '04','d' union all select '05','e' --> 测试数据:[B2] if object_id('[B2]') is not null drop table [B2] create table [B2]( [id] int, [内容] varchar(11) ) insert [B2] select 1,'01,05' union all select 2,www.xu168.com'02' union all select 3,'01,03' union all select 4,'02,05' union all select 5,'01,02,03' union all select 6,'01,02,04,05' union all select 7,'02,04' go
with t as( select b.id, a.内容 from [B2] b inner join [A1] a on CHARINwww.1586f.comDEX(a.编码,b.内容)>0