asp.net - SQL Get Rows With A Similar Column Value -
i have database stores duplicate rows, duplicate not clear cut, e.g. following 2 column values duplicate:
g12345 & g1234 --> because similar (a string comparison shows characters match 83.3%).
i need writing sql
query retrieve values similar string sent part of query, e.g. on 50% of characters matched.
can this? have c# method follows not quite sure how accomplish in sql:
static double stringcompare(string a, string b) { if (a == b) //same string, no iteration needed. return 100; if ((a.length == 0) || (b.length == 0)) //one empty, second not { return 0; } var maxlen = a.length > b.length ? a.length : b.length; var minlen = a.length < b.length ? a.length : b.length; var samecharatindex = 0; (var = 0; < minlen; i++) //compare char char { if (a[i] == b[i]) { samecharatindex++; } } return samecharatindex / maxlen * 100; }
thanks in advance.
not sure if trying use sql-server or mysql, create , use following function in sql-server:
create function stringcompare (@a nvarchar(200), @b nvarchar(200) ) returns float begin if ( @a = @b or (@a null , @b null) ) begin return 100.0 end if ( ((@a null or len(@a) = 0) , (@b not null , len(@b) > 0)) or ((@b null or len(@b) = 0) , (@a not null , len(@a) > 0)) ) begin return 0.0 end declare @maxlen int set @maxlen = case when len(@a) > len(@b) len(@a) else len(@b) end declare @minlen int set @minlen = case when len(@a) < len(@b) len(@a) else len(@b) end declare @samecharatindex int set @samecharatindex = 0 declare @count int set @count = 1 while (@count <= @minlen) begin if (substring(@a, @count, 1) = substring(@b, @count, 1)) begin set @samecharatindex = @samecharatindex + 1 end set @count = @count + 1 end return cast(@samecharatindex float) / cast(@maxlen float) * 100.0 end
which used in statement follows:
select dbo.stringcompare('test', 'test'), dbo.stringcompare('nope', 'test'), dbo.stringcompare('partial', 'parsomethingelse')
please note, having loop in sql running on many records can inefficient. , may want consider whether have in sql.
Comments
Post a Comment