Sunday, December 6, 2026

Close Enough? SQL Server Gets Fuzzy.

Ever had to identify duplicate records in a database where the names aren't quite the same? An extra character here, a typo there -- and our trusty equality comparison misses them entirely.

On November 17, Microsoft announced general availability of four fuzzy string matching functions. No custom functions required.

I can think of quite a few occasions over the years when these would have come in handy, particularly when hunting down duplicates in some rather dirty data.

For SQL Server, general availability begins with SQL Server 2025 CU11. The functions are also available in Azure SQL Database, Azure SQL Managed Instance (SQL Server 2025 or Always-up-to-date update policy), and SQL database in Microsoft Fabric.

1. A Little Test Data

For this demo, we have eight vendor records. Some have spelling errors, some have similar names and some have identical names.

CREATE TABLE #FuzzyVendors
(
    VendorID INT PRIMARY KEY,
    VendorName NVARCHAR(100) COLLATE Latin1_General_100_CI_AS_SC NOT NULL
);

INSERT #FuzzyVendors (VendorID, VendorName)
VALUES
(1, N'Anderson Supply'),
(2, N'Andreson Supply'),
(3, N'Anderson Suplly'),
(4, N'Anderson Supplies'),
(5, N'Anderson Electric'),
(6, N'Anderson Supply'),
(7, N'Chicago Hardware'),
(8, N'Anderson Supply');

SELECT * FROM #FuzzyVendors;

2. What Are We Missing With an Exact Match?

SELECT VendorID, VendorName
FROM #FuzzyVendors
WHERE VendorName = 'Anderson Supply'
ORDER BY VendorID;

Our query returns vendors 1, 6 and 8, but misses vendors 2 and 3 because of a typo in each of their names. How closely do those names actually match 'Anderson Supply'?

3. Four Functions. Same Data.

We know that vendors 2 and 3 are just one typo away from 'Anderson Supply'. Can we identify them without knowing what or where those typos are?

Microsoft has given us four functions to work with, based on two different string matching algorithms:

  • Edit Distance - How many character edits would it take to make two strings match?
  • Jaro-Winkler - How closely do two strings resemble one another, taking into consideration matching characters, their positions and similarities at the beginning of the strings?

Each algorithm has a distance and a similarity function. With distance, we're looking for lower values. With similarity, we're looking for higher values.

Function Returns Exact Match
EDIT_DISTANCE (ED) Number of edits 0
EDIT_DISTANCE_SIMILARITY (ES) Score of 0-100 100
JARO_WINKLER_DISTANCE (JWD) Score of 0-1 0
JARO_WINKLER_SIMILARITY (JWS) Score of 0-100 100

Let's compare each of our eight vendor names to 'Anderson Supply' using all four functions. I'm particularly interested in how differently the two algorithms score the same names.

DECLARE @VendorName NVARCHAR(100) = N'Anderson Supply';

SELECT VendorID, VendorName,
    EDIT_DISTANCE(VendorName, @VendorName) AS ED,
    EDIT_DISTANCE_SIMILARITY(VendorName, @VendorName) AS ES,
    JARO_WINKLER_DISTANCE(VendorName, @VendorName) AS JWD,
    JARO_WINKLER_SIMILARITY(VendorName, @VendorName) AS JWS
FROM #FuzzyVendors
ORDER BY VendorID;

Look at these results, particularly how differently the two algorithms score vendors 4 and 5.

Edit Distance and Similarity

Look at vendors 2 and 3. In one we've got two letters reversed in 'Andreson', in the other we have an incorrect letter in 'Suplly'. These are the two typos that our original query missed, and both have an edit distance of 1.

EDIT_DISTANCE tells us how many character edits are required to make the strings match. EDIT_DISTANCE_SIMILARITY takes that distance and the length of the longer string into consideration, returning a score between 0 and 100. Remember, we're looking for lower EDIT_DISTANCE and higher EDIT_DISTANCE_SIMILARITY values.

With this in mind, I can see using EDIT_DISTANCE to identify data entry errors in product codes, order numbers or other identifiers where a single incorrect character is a bad thing. On the other hand, the EDIT_DISTANCE_SIMILARITY function would be more useful when comparing strings of varying lengths.

Jaro-Winkler Distance and Similarity

Now, look at vendors 4 and 5. 'Anderson Supplies' has a Jaro-Winkler similarity (JWS) score of 95 and 'Anderson Electric' has a JWS score of 85. Their edit similarity scores, however, are 82 and 53, respectively. Why such a difference?

With Jaro-Winkler, we're not counting character edits. We're looking at how many characters the strings have in common, their order and, in particular, how closely the strings match at the beginning. The fact that both vendor names begin with 'Anderson' contributes to their higher JWS scores.

Consider how useful this could be when searching for a customer by name. Maybe we only have part of the name or we're unsure of its spelling. A high JWS score could help us find the customer we're looking for.

What I find interesting is that 'Anderson Electric' has a JWS score of 85 despite requiring eight character edits to match 'Anderson Supply'. That's important. Depending on what we're looking for, the chosen algorithm could make quite a difference in the records returned.

Suppose we're only interested in vendor names that are no more than two character edits away from 'Anderson Supply'. We don't need to care how far apart the other names are, do we?

4. How Many Edits Are We Willing to Accept?

EDIT_DISTANCE has an optional third parameter, maximum_distance, for this purpose. It is an integer value and it specifies the maximum edit distance to calculate. In this query I am making two calls to EDIT_DISTANCE; the first without a limit and the second with a limit of 2.

SELECT VendorID, VendorName,
    EDIT_DISTANCE(VendorName, N'Anderson Supply') AS FullDistance,
    EDIT_DISTANCE(VendorName, N'Anderson Supply', 2) AS LimitedDistance
FROM #FuzzyVendors
ORDER BY VendorID;

The first call will return the actual edit distance for each vendor name. The second will return the actual distance only if it is less than or equal to the maximum_distance limit of 2. Anything else gets maximum_distance + 1, regardless. The benefit here is that SQL Server may be able to stop processing a string without having to calculate its full edit distance.

5. A Practical Application - Vendor Data Imports

Suppose we're consolidating vendor data from two different ERP systems. Before importing the data, we need to identify vendors that may already exist in the target system. An exact match on the vendor name isn't necessarily going to find them.

I believe this is a good use-case for our new fuzzy string matching functions. We could compare the incoming vendor names to the existing vendor names and review potential matches before importing the data.

Here I've created a small table representing our incoming data, with an exact match, a misspelled vendor name, a variation of an existing vendor name and an entirely new vendor.

CREATE TABLE #IncomingVendors
(
    VendorID INT PRIMARY KEY,
    VendorName NVARCHAR(100) COLLATE Latin1_General_100_CI_AS_SC NOT NULL
);

INSERT #IncomingVendors (VendorID, VendorName)
VALUES
(101, N'Chicago Hardware'),
(102, N'Andersen Supply'),
(103, N'Anderson Supply Inc'),
(104, N'Midwest Industrial');

SELECT * FROM #IncomingVendors;

Let's use two of our new functions in the same query. I'm looking for vendor names that are no more than two character edits apart or have a Jaro-Winkler similarity score of 95 or greater.

SELECT 
    i.VendorID AS IncomingID,
    i.VendorName AS IncomingVendor,
    v.VendorID AS ExistingID,
    v.VendorName AS ExistingVendor,
    EDIT_DISTANCE(i.VendorName, v.VendorName) AS ED,
    JARO_WINKLER_SIMILARITY(i.VendorName, v.VendorName) AS JWS
FROM #IncomingVendors i
CROSS JOIN #FuzzyVendors v
WHERE 2 >= EDIT_DISTANCE(i.VendorName, v.VendorName, 2)
   OR JARO_WINKLER_SIMILARITY(i.VendorName, v.VendorName) >= 95
ORDER BY IncomingID, ED, JWS DESC, ExistingID;

Note the OR condition in my WHERE clause. By using both algorithms, we can pick up variations in vendor names that would be missed using EDIT_DISTANCE alone.

* Also be mindful of the CROSS JOIN, which produces a Cartesian product of the two tables. With four incoming vendors and eight existing vendors, we only have 32 possible vendor pairs (4 x 8). But what if we're importing 10K vendors into a table of 100K existing vendors? That's a billion potential vendor pairs (10,000 x 100,000) to evaluate. In a production environment, I'd want to narrow down the candidate records wherever possible before applying the fuzzy string matching functions.

This call compares the existing vendor data in #FuzzyVendors with #IncomingVendors to identify potential duplicates. 'Midwest Industrial' has no qualifying match, so it is not in our return. We did, however, find an exact match for 'Chicago Hardware' and potential matches for both 'Andersen Supply' and 'Anderson Supply Inc'. This is where we'd review other attributes of the potential duplicates before importing the data.

I can see using this with an ETL process to flag potential matches before overwriting existing data.

And those thresholds of 2 and 95? Those are just my choices for this test. I would definitely experiment with those values against actual vendor data before running this in production.

6. A Note About Collation

Did you notice that I specified a Windows collation on the VendorName column when creating our test tables? This is necessary because the new fuzzy string matching functions do not support non-binary SQL collations, including the widely used SQL_Latin1_General_CP1_CI_AS.

If you're working with existing data and encounter error 9847, you don't need to change the collation of your columns. You can use the COLLATE clause within your fuzzy string matching function calls to apply a supported Windows collation for the comparison.

-- like this
SELECT EDIT_DISTANCE(
    N'Anderson Supply' COLLATE Latin1_General_100_CI_AS_SC,
    N'Andreson Supply' COLLATE Latin1_General_100_CI_AS_SC) AS Edits;

Cleanup

DROP TABLE IF EXISTS #FuzzyVendors;
DROP TABLE IF EXISTS #IncomingVendors;

Where I Landed

I like the idea of using these functions to identify duplicates during an import, or to help clean up dirty data. But you've got to remember that the chosen algorithm could make quite a difference in the data returned.

A key distinction comes down to where and how typos happen. EDIT_DISTANCE treats every character position equally, while JARO_WINKLER gives more weight to matching prefixes. Picking the wrong algorithm doesn't just shuffle your search rankings. It can also very silently change which records survive your WHERE clause, and turn valid matches into missing data.

Lastly, because these are scalar functions applied to a column inside a WHERE or JOIN predicate, they fall under SQL Server's standard SARGability rules. That means that these predicates are non-SARGable and the comparisons can become computationally heavy. Treat them like standard = operators and you could very quickly impact the performance of your server.

For now, I will use the fuzzy string matching functions to help find the records that need a closer look. I will not use them, by themselves, to decide which records to delete.

More to Read

Microsoft Learn - Fuzzy string matching
Microsoft Learn - EDIT_DISTANCE
Microsoft Learn - EDIT_DISTANCE_SIMILARITY
Microsoft Learn - JARO_WINKLER_DISTANCE
Microsoft Learn - JARO_WINKLER_SIMILARITY
SQL, CODE, COFFEE, ETC. - Fuzzy string matching (SQL Server 2025)

No comments:

Post a Comment