两个字符串,以特定符号分隔(例如‘,’号),求交集
第一种情况:
declare @m varchar(100),@n varchar(100)
select
@m=',2,3,5,7,8,9,10,',
@n=',1,3,6,8,10,'
select
--count(1)
result=substring(@m,number,charindex(',',@m,number)-number)
from master..spt_values
where number<len(@m) and type='p' and number>1
and substring(','+@m,number,1)=','
and charindex(substring(@m,number-1,charindex(',',@m,number)-number+2),@n)>0
第二种情况:
declare @m varchar(100),@n varchar(100)
select
@m='2,3,5,7,8,9,10',
@n='1,3,6,8,10'
select
--COUNT(1)
result=substring(@m,number,charindex(',',@m+',',number)-number)
from master.dbo.spt_values
where type='p' and number>1 and number<len(@m)
and substring(','+@m,number,1)=','
and charindex(substring(@m,number-1,charindex(',',@m+',',number)-number + 2),','+@n)>0
以上两种情况,如果把“result=”那一行的sql语句换成count(1),则可以用来判断两个字符串是否有交集,count的结果值大于0就是有交集(count的数值就是交集的字符个数)
如果要把所有交集的字符都输出,那么就用“result=”那一行就可以了。