Here's a simple User-Defined Function implementation of the
Hamming Distance Algorithm for SQL Server:
create function HamDist(@value1 char(8000), @value2 char(8000))
returns int
as
begin
declare @distance int
declare @i int
declare @len int
select @distance = 0,
@i =1,
@len = case when len(@value1) > len(@value2)
then len(@value1)
else len(@value2) end
if (@value1 is null) or (@value2 is null)
return null
while (@i <= @len)
select @distance = @distance +
case when substring(@value1,@i,1) != substring(@value2,@i,1)
then 1
else 0 end,
@i = @i +1
return @distance
end
Note that the inputs are cast to char(8000) so that the strings are padded to always be equal length if necessary, but we use the larger LEN() of the two values to ensure that we only check the necessary characters. If either input is NULL, then NULL is returned.
This could be done set-based as well, but I am not sure if you will get much of a benefit -- this short loop through the string values passed should be pretty efficient.
Enjoy!