Skip to content
Advertisement

How to compare two strings based on percent match in SQL

I want to post a solution to a interesting problem I was facing in T-SQL.

The problem: Compare two string fields based on a percent match. In addition, the two strings may have the words in them translocated.

For example: “Joni Bravo” and “Bravo Joni”. These two strings should return a match of 100%, which means that position is not relevant. Few more things worth noting are that this code is made to compare strings that have space as delimiter in them. If the first string doesnt have space the match is set to 100% without actual check. This was not developed, because the strings this function is ment to compare always contain two or more words. Also, it is written on MS SQL Server 2017 if that mathers.

Advertisement

Answer

So here is the solution, hope this helps anyone 🙂 gl

PS: I decided to write it in a user-defined function just for the needs of my project.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement