/ ************************************************** ************** File: fncompare.sql ** name: fncompare ** Description: Compare Two strings, return the alike degree with the percentage. Compare two strings, return to the same degree percentage. ** Return Values: @Result ** parameters: @ CHVA, @ chvb ** Author: Godness ** Date: 2005-1-12 ** Use method: select dbo.fncompare ('Abcdefg', 'DifdewG " ******************************************************************* ************ /
Create Function Fncompare (@chva varchar (8000), @ chvb varchar (8000)) Returns var, @ Same Decimal (10, 2), @ surult var, @ result var Same = 0 if len (@chva)> = len (@CHVB) SELECT @ i = len (@chva), @ j = len (@CHVB) else select @ i = len (@CHVB), @ J = LEN @CHVA) While @J> 0 Begin if Substring (@ chava, @ J, 1) = Substring (@ CHVB, @ J, 1) SELECT @ Same = @ Same 1 set @ J = @ J-1 End Select @ Result = (Cast (Cast (@ Same * 100 / @ i as varchal) '%') Return @Resultend