我想发布一个解决方案,解决我在T-SQL中遇到的一个有趣问题。
问题: 根据匹配百分比比较两个字符串字段。 此外,这两个字符串可能包含易位中的单词。
例如:“Joni Bravo”和“Bravo Joni”。这两个字符串应该返回100%的匹配,这意味着该位置不相关。还有一些值得注意的事情是,这个代码用于比较具有空格作为分隔符的字符串。如果第一个字符串没有空格,则匹配设置为100%而不进行实际检查。这没有开发,因为这个函数要比较的字符串总是包含两个或更多的单词。此外,如果匹配,它将写在MS SQL Server 2017上。
答案 0 :(得分:1)
所以这是解决方案,希望这有助于任何人:) gl
/****** Object: UserDefinedFunction [dbo].[STRCOMP] Script Date: 29/03/2018 15:31:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[STRCOMP] (
-- Add the parameters for the function here
@name_1 varchar(255),@name_2 varchar(255)
)
RETURNS float
AS
BEGIN
-- Declare the return variable here
declare @p int = 0;
declare @c int = 0;
declare @br int = 0;
declare @p_temp int = 0;
declare @emergency_stop int = 0;
declare @fixer int = 0;
declare @table1_temp table (
row_id int identity(1,1),
str1 varchar (255));
declare @table2_temp table (
row_Id int identity(1,1),
str2 varchar (255));
declare @n int = 1;
declare @count int = 1;
declare @result int = 0;
declare @total_result float = 0;
declare @result_temp int = 0;
declare @variable float = 0.0;
set @name_1 = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@name_1,'!',''),' ',' '),'1',''),'2',''),'3',''),'4',''),'5',''),'0',''),'6',''),'7',''),'8',''),'9','');
set @name_2 = REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@name_2,'!',''),' ',' '),'1',''),'2',''),'3',''),'4',''),'5',''),'0',''),'6',''),'7',''),'8',''),'9','');
set @c = charindex(' ',substring(@name_1,@p,len(@name_1)));
IF(@c = 0)
BEGIN
RETURN 100.00
END;
WHILE (@br != 2 or @emergency_stop = 20)
BEGIN
insert into @table1_temp(str1)
select substring (@name_1,@p,@c);
set @p = len(substring (@name_1,@p,@c))+2;
set @p = @p + @p_temp - @fixer;
set @p_temp = @p;
set @c = CASE WHEN charindex(' ',substring(@name_1,@p,len(@name_1))) = 0 THEN len(@name_1) ELSE charindex(' ',substring(@name_1,@p,len(@name_1))) END;
set @fixer = 1;
set @br = CASE WHEN charindex(' ',substring(@name_1,@p,len(@name_1))) = 0 THEN @br + 1 ELSE 0 END;
set @emergency_stop = @emergency_stop +1;
END;
set @p = 0;
set @br = 0;
set @emergency_stop = 0;
set @fixer = 0;
set @p_temp = 0;
set @c = charindex(' ',substring(@name_2,@p,len(@name_2)));
WHILE (@br != 2 or @emergency_stop = 20)
BEGIN
insert into @table2_temp(str2)
select substring (@name_2,@p,@c);
set @p = len(substring (@name_2,@p,@c))+2;
set @p = @p + @p_temp - @fixer;
set @p_temp = @p;
set @c = CASE WHEN charindex(' ',substring(@name_2,@p,len(@name_2))) = 0 THEN len(@name_2) ELSE charindex(' ',substring(@name_2,@p,len(@name_2))) END;
set @fixer = 1;
set @br = CASE WHEN charindex(' ',substring(@name_2,@p,len(@name_2))) = 0 THEN @br + 1 ELSE 0 END;
set @emergency_stop = @emergency_stop +1;
END;
WHILE((select str1 from @table1_temp where row_id = @n) is not null)
BEGIN
set @count = 1;
set @result = 0;
WHILE((select str2 from @table2_temp where row_id = @count) is not null)
BEGIN
set @result_temp = DIFFERENCE((select str1 from @table1_temp where row_id = @n),(select str2 from @table2_temp where row_id = @count));
IF(@result_temp > @result)
BEGIN
set @result = @result_temp;
END;
set @count = @count + 1;
END;
set @total_result = @total_result + @result;
set @n = @n + 1;
END;
set @variable = (select @total_result / (select max(row_count) from (
select max(row_id) as row_count from @table1_temp
union
select max(row_id) as row_count from @table2_temp) a));
RETURN @variable/4 * 100;
END
GO
PS:我决定在用户定义函数中编写它,只是为了满足我的项目需求。