我需要使用T-SQL逐个字符地比较两个字符串。假设我有两个类似的字符串:
123456789 212456789
每当字符不匹配时,我想增加变量@Diff + = 1。在这种情况下,前三个字符不同。因此,@ Diff = 3(默认值为0)。
感谢您的所有建议。
对于您不想使用逐行方法的表中的列,请尝试以下一种方法:
with cte(n) as ( select 1 union all select n + 1 from cte where n < 9 ) select t.s1, t.s2, sum( case when substring(t.s1, c.n, 1) <> substring(t.s2, c.n, 1) then 1 else 0 end ) as diff from test as t cross join cte as c group by t.s1, t.s2
= > SQL小提琴演示