有两个字符串,比较它们的不同部分。例如:
字符串1:"A-B-c;A-B-D;D-F-G;F-H-J"
字符串2:"A-B-c;A-B-D;D-F-G;F-H-J;Y-U-O;T-R-E"
两个字符串相比较,找出不同的部分,用存储过程该怎么做,例如上面的应该找出的字符串为"Y-U-O;T-R-E"
字符串1:"A-B-c;A-B-D;D-F-G;F-H-J"
字符串2:"A-B-c;A-B-D;D-F-G;F-H-J;Y-U-O;T-R-E"
两个字符串相比较,找出不同的部分,用存储过程该怎么做,例如上面的应该找出的字符串为"Y-U-O;T-R-E"
+1patindex/charindex 也可以处理,LZ参考
1楼的方法有个问题
select replace('abcd','ab','')
select replace('ab','abcd','')
这两句的结果不一样
CREATE proc [dbo].[CompareString]
@StringOne nvarchar(max),
@StringTwo nvarchar(max)
as
if LEN(@StringOne)>=LEN(@StringTwo)
begin
select substring(@StringOne,len(@StringTwo)+1,abs(len(@StringTwo)-len(@StringOne)))
end
else
begin
select substring(@StringTwo,len(@StringOne)+1,abs(len(@StringOne)-len(@StringTwo)))
end
GO
@StringOne nvarchar(max),
@StringTwo nvarchar(max)
as
if LEN(@StringOne)>=LEN(@StringTwo)
begin
select replace((@StringOne,@StringTwo, '')
end
else
begin
select replace(@StringTwo,@StringOne, '')
end
GO
两者结合起来就可以啦
exec CompareString 'A-B;A-C','D-F;A-C;F-G;A-B'
得出的结果为“;F-G;A-B”
select
*
from
a
where
not exists(select 1 from b where charindex(';'+a+';',';'+b+';)>0)
*
from
a
where
exists(select 1 from b where charindex(';'+a+';',';'+b+';')>0)