逐个字符比较两个字符串并突出显示不同的字符串

Compare two string char by char and highlight the different ones

我需要一个函数,它将接受 2 个字符串,return 第二个字符串用 html 代码突出显示不匹配的字符。 例如:

dbo.function('Jack', 'jake')

将return一个字符串:

'ja<font color=red>k</font><font color=red>e</font>'

这是一个可以转换为函数的工作示例

Declare @S1 varchar(max) = 'Jack'
Declare @S2 varchar(max) = 'jake'

Declare @Return varchar(max) = ''

Select @Return = @Return + case when S2=S1 then S2
                       else '<font color=red>'+S2+'</font>'
                  end
 From (
        Select N
              ,S1 = substring(@S1,N,1)
              ,S2 = substring(@S2,N,1)
         From ( Select Top 1000 N=Row_Number() Over (Order By (Select NULL)) 
                 From master..spt_values n1, master..spt_values n2
              ) A
         Where N<=len(@S1) or N<=len(@S2)
      ) A
 Order by N

Select @Return

结果

ja<font color=red>k</font><font color=red>e</font>