sqlsql-serversequencematcher

Is there an equivalent to pythons's SequenceMatcher in SQL Server to join on columns that are similar?


In python there a nice built in function that lets me check the difference between the sequence of two strings. Example below:

from difflib import SequenceMatcher

def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()

Example:

similar("Apple","Appel")
0.8
similar("Apple","Mango")
0.0

Is there an equivalent function in SQL?

What I need to do is join two tables on a column. On table A, this column is their actual account number, on the other table the column is the user adding their account number which isn't a required field and because of that I've noticed that there are slight variations in their actual account number.

Table A

account_num   Name
5WWW55        John
DDDDD7        Jacob
SSSSX7        jingleheimer

Table B

account_num   Name
5WWW55        John
3SSSX7        jingleheimer     -- First character is different 


Select Table_A.account_num, 
       Table_b.Name 
FROM Table_A
JOIN TABLE_B 
          on Table_A.account_num = Table_B.account_num

Expected output:

account_num   Name
5WWW55        John
SSSSX7        jingleheimer

Solution

  • You could use DIFFERENCE:

    SELECT Table_A.account_num, 
           Table_b.Name 
    FROM Table_A
    JOIN TABLE_B 
      ON DIFFERENCE(Table_A.account_num , Table_B.account_num) = 4;
    

    db<>fiddle