IF object_id('F_strcompare') IS NOT NULL BEGIN PRINT 'Dropping function F_strcompare' DROP FUNCTION F_strcompare IF @@ERROR = 0 PRINT 'Function F_strcompare dropped ' END go CREATE FUNCTION F_strcompare(@str1 VARCHAR(8000), @str2 VARCHAR(8000)) RETURNS VARCHAR(10) AS BEGIN DECLARE @i INT, @j INT, @k INT, @ret VARCHAR(10) SELECT @i = MAX(strlen), @j = MIN(strlen), @k = 0 FROM (SELECT strlen=Len(@str1) UNION SELECT strlen=Len(@str2)) t IF( @j = 0 ) RETURN '0%' WHILE @j > 0 BEGIN IF Substring(@str1, @j, 1) = Substring(@str2, @j, 1) SET @k=@k + 1 SET @j=@j - 1 END SET @ret = Rtrim(CAST(@k * 100.0 / @i AS NUMERIC(3, 0))) + '%' RETURN @ret END GO SELECT dbo.F_strcompare('中国人','中华人民共和国')