forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathudf_StringComparisonsLevenshteinFull-matrix.sql
More file actions
50 lines (47 loc) · 2.11 KB
/
udf_StringComparisonsLevenshteinFull-matrix.sql
File metadata and controls
50 lines (47 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
alter FUNCTION LevenschteinDifference
(
@FirstString nVarchar(255), @SecondString nVarchar(255)
)
RETURNS int
as begin
Declare @PseudoMatrix table
(location int identity primary key,
firstorder int not null,
Firstch nchar(1),
secondorder int not null,
Secondch nchar(1),
Thevalue int not null default 0,
PreviousRowValues varchar(200)
)
insert into @PseudoMatrix (firstorder, firstch, secondorder, secondch, TheValue )
SELECT TheFirst.number,TheFirst.ch, TheSecond.number,TheSecond.ch,0
FROM --divide up the first string into a table of characters/sequence
(SELECT number, SUBSTRING(@FirstString,number,1) AS ch
FROM numbers WHERE number <= LEN(@FirstString) union all Select 0,Char(0)) TheFirst
cross JOIN --divide up the second string into a table of characters/sequence
(SELECT number, SUBSTRING(@SecondString,number,1) AS ch
FROM numbers WHERE number <= LEN(@SecondString) union all Select 0,Char(0)) TheSecond
--ON Thefirst.ch= Thesecond.ch --do all valid matches
order by TheFirst.number, TheSecond.number
Declare @current Varchar(255)
Declare @previous Varchar(255)
Declare @TheValue int
Declare @Deletion int, @Insertion int, @Substitution int, @minim int
Select @current='', @previous=''
Update @PseudoMatrix
Set
@Deletion=@TheValue+1,
@Insertion=ascii(substring(@previous,secondorder+1,1))+1,
@Substitution=ascii(substring(@previous,(secondorder),1)) +1,
@minim=case when @Deletion<@Insertion then @Deletion else @insertion end,
@TheValue = Thevalue = case --when Firstorder+SecondOrder=0 then 0
when SecondOrder=0 then FirstOrder
When FirstOrder=0 then Secondorder
when FirstCh=SecondCh then ascii(substring(@previous,(secondorder),1))
else case when @Minim<@Substitution then @Minim else @Substitution end
end,
@Previous=PreviousRowValues=case when secondorder =0 then @current else @Previous end,
@current= case when secondorder =0 then char(@TheValue) else @Current+char(@TheValue) end
return @TheValue
End
Go