The below query compares, shows the different characters and bring you the count of differences
Declare @char1 nvarchar(1), @char2 nvarchar(1), @i int = 1, @max int
Declare @string1 nvarchar(max) = '123456789'
, @string2 nvarchar(max) = '212456789'
Declare @diff_table table (pos int , string1 nvarchar(50) , string2 nvarchar(50), Status nvarchar(50))
Set @max = (select case when len(@String1+'x')-1 > len(@string2+'x')-1 then len(@String1+'x')-1 else len(@string2+'x')-1 end)
while @i < @max +1
BEGIN
Select @char1 = SUBSTRING(@string1,@i,1), @char2 = SUBSTRING(@string2,@i,1)
INSERT INTO @diff_table values
(
@i,
case when UNICODE(@char1) is null then '' else concat(@char1,' - (',UNICODE(@char1),')') end,
case when UNICODE(@char2) is null then '' else concat(@char2,' - (',UNICODE(@char2),')') end,
case when ISNULL(UNICODE(@char1),0) <> isnull(UNICODE(@char2),0) then 'CHECK' else 'OK' END
)
set @i+=1
END
Select * from @diff_table
Declare @diff int = (Select count(*) from @diff_table where Status = 'Check')
Select @diff 'Difference'
The output will be like this:
