T-SQL donne le pourcentage de caractère match de 2 cordes

Disons que j'ai un set de 2 mots:

Alexander et Alecsander OU Alexander et Alegzander

Alexander et Aleaxnder, ou toute autre combinaison. En général, nous parlons de l'homme d'erreur en tapant un mot ou un ensemble de mots.

Ce que je veux réaliser est d'obtenir le pourcentage de correspondance des caractères des 2 chaînes.

Voici ce que j'ai à ce jour:

    DECLARE @table1 TABLE
(
  nr INT
  , ch CHAR
)

DECLARE @table2 TABLE
(
  nr INT
  , ch CHAR
)


INSERT INTO @table1
SELECT nr,ch FROM  [dbo].[SplitStringIntoCharacters] ('WORD w') --> return a table of characters(spaces included)

INSERT INTO @table2
SELECT nr,ch FROM  [dbo].[SplitStringIntoCharacters] ('WORD 5')

DECLARE @resultsTable TABLE
( 
 ch1 CHAR
 , ch2 CHAR
)
INSERT INTO @resultsTable
SELECT DISTINCt t1.ch ch1, t2.ch ch2 FROM @table1 t1
FULL JOIN @table2 t2 ON  t1.ch = t2.ch  --> returns both matches and missmatches

SELECT * FROM @resultsTable
DECLARE @nrOfMathches INT, @nrOfMismatches INT, @nrOfRowsInResultsTable INT
SELECT  @nrOfMathches = COUNT(1) FROM  @resultsTable WHERE ch1 IS NOT NULL AND ch2 IS NOT NULL
SELECT @nrOfMismatches = COUNT(1) FROM  @resultsTable WHERE ch1 IS NULL OR ch2 IS NULL


SELECT @nrOfRowsInResultsTable = COUNT(1)  FROM @resultsTable


SELECT @nrOfMathches * 100 / @nrOfRowsInResultsTable

La SELECT * FROM @resultsTable sera de retour le suivant:

ch1         ch2
NULL        5
[blank]     [blank] 
D           D
O           O
R           R
W           W
Et quel est le problème avec elle? Que le code de travail est-elle correcte?
Qu'il n'est pas précise.

OriginalL'auteur Dragos Durlut | 2011-12-15