USE pubs
GO
-- Returns a DIFFERENCE value of 4, the least possible difference.
SELECT SOUNDEX('Green'),
SOUNDEX('Greene'), DIFFERENCE('Green','Greene')
GO
-- Returns a DIFFERENCE value of 0, the highest possible difference.
SELECT SOUNDEX('Blotchet-Halls'),
SOUNDEX('Greene'), DIFFERENCE('Blotchet-Halls', 'Greene')
GO
Create function test(@str1 varchar(100),@str2 varchar(10))
returns int
as
begin
declare @i int,@j int
set @i =len(@str2)
while @i > 0
begin
if charindex(left(@str2,@i),@str1) > 0
or charindex(right(@str2,@i),@str1) > 0
begin
set @j = @i
set @i = 0
end
set @i = @i-1
end
return @j
end
declare @str varchar(10)
set @str = 'fengufo'
select id,dbo.test(str,@str) as 匹配长度 from t1 order by dbo.test(str,@str) desc,id
id 匹配长度
----------- -----------
1 7
4 7
2 4
3 3