Fuzzy matching without CLR

Views 48

Every organisation I work with has the same customer in its database three times: “Jon Smith”, “John Smith” and “John Smiht”. Regex can tell you whether a value is well formed. It can’t tell you that two values are probably the same thing. That’s the job of fuzzy string matching, and SQL Server 2025 now has it built in.

The four new functions

They’re based on two well-known algorithms, each with a “distance” version and a “similarity” version:

Function Algorithm Returns Read it as
EDIT_DISTANCE Damerau-Levenshtein (see below) Integer Number of edits needed to turn one string into the other; 0 means identicle
EDIT_DISTANCE_SIMILARITY Damerau-Levenshtein (see below) 0-100 100 means identical
JARO_WINKLER_DISTANCE Jaro-Winkler 0.0-1.0 0 means identical
JARO_WINKLER_SIMILARITY Jaro-Winkler 0-100 100 means identical

In plain terms, Damerau-Levenshtein counts how many single-character edits you’d need: inserting, deleting or changing a character, or swapping two adjacent characters. Jaro-Winkler gives extra weight to strings that match at the start, which makes it good for people’s names and short codes.

First, check whether you need the preview switch

Microsoft Learn says these functions are in preview and only work once the PREVIEW_FEATURES database scoped configuration is on. On my test instance (SQL Server 2025 CU__) they ran with it switched off. Check your own build before you change anything:


-- Is the preview switch on? (0 = off)
SELECT name, value
FROM sys.database_scoped_configurations
WHERE name = 'PREVIEW_FEATURES';

-- Do the fuzzy functions run anyway?
SELECT EDIT_DISTANCE('Smith' COLLATE Latin1_General_100_CI_AS,
                     'Smiht' COLLATE Latin1_General_100_CI_AS) AS ED;

-- Only if the SELECT above fails on your build:
-- ALTER DATABASE SCOPED CONFIGURATION SET PREVIEW_FEATURES = ON;

 

If your build does need it, remember that the setting turns on every preview feature in the database, not just these four functions. On a production database, that’s a change-control conversation, not a quick tweak. My advice is to start with a copy of the data, or a dedicated data-quality database, rather than flipping it on in the main OLTP database.

The collation catch

The first query I ran in my normal test database failed:


Msg 9847, Level 16, State 1
The fuzzy string matching function does not support SQL_* collations. Use a Windows collation instead.

The fuzzy functions reject any input in a legacy SQL_ collation, and SQL_Latin1_General_CP1_CI_AS is still the default collation on a huge number of installs. Check yours:


SELECT SERVERPROPERTY('Collation')                  AS ServerCollation,
       DATABASEPROPERTYEX(DB_NAME(), 'Collation')   AS DatabaseCollation;

String literals take the database’s collation, so even JARO_WINKLER_SIMILARITY('Smith', 'Smiht') fails in a database with a SQL collation. You have three ways round it:

1. Add a COLLATE clause to the inputs. This is the least invasive fix and has no side effects elsewhere:


SELECT JARO_WINKLER_SIMILARITY(
         a.FullName COLLATE Latin1_General_100_CI_AS,
         b.FullName COLLATE Latin1_General_100_CI_AS) AS NameScore
FROM ...

2. Do the matching in a separate data-quality database created with a Windows collation, and copy the columns you need into it. This fits the batch-job approach I recommend below anyway.

3. Change the database or column collation. Don’t do this lightly: it affects comparisons, temp table joins with tempdb, and every query that touches those columns.

Microsoft’s documentation notes that collation rules aren’t yet implemented for these functions and that their output might change once they are. That’s another reason to keep fuzzy matching out of production logic while it’s in preview. The examples in the rest of this post include the COLLATE clause, so they run whatever your database collation is.

A quick feel for the scores


SELECT EDIT_DISTANCE('Smith' COLLATE Latin1_General_100_CI_AS, 'Smiht' COLLATE Latin1_General_100_CI_AS)            AS ED,
       EDIT_DISTANCE_SIMILARITY('Smith' COLLATE Latin1_General_100_CI_AS, 'Smiht' COLLATE Latin1_General_100_CI_AS) AS ED_Sim,
       JARO_WINKLER_DISTANCE('Smith' COLLATE Latin1_General_100_CI_AS, 'Smiht' COLLATE Latin1_General_100_CI_AS)    AS JW,
       JARO_WINKLER_SIMILARITY('Smith' COLLATE Latin1_General_100_CI_AS, 'Smiht' COLLATE Latin1_General_100_CI_AS)  AS JW_Sim;

Here’s what that returned on my test instance (SQL Server 2025 CU__):

ED ED_Sim JW JW_Sim
1 80 0.0467 95

One thing caught me out. Microsoft Learn says EDIT_DISTANCE doesn’t support transpositions, which would make swapping two adjacent letters, as in “Smiht”, cost two edits. On my build it returned 1, so a swap counted as a single edit, which is Damerau-Levenshtein behaviour. Either the documentation is behind or a cumulative update changed it. Check on your own build before you pick thresholds, because a typo-heavy data set will score quite differently. Jaro-Winkler still scores this typo more kindly (95 against 80), helped by the matching “Smi” prefix.

Finding duplicate customers

This is the query most people want. Compare each customer with every other customer and keep the close matches:


SELECT a.CustomerID, a.FullName,
       b.CustomerID AS DupCustomerID, b.FullName AS DupFullName,
       JARO_WINKLER_SIMILARITY(a.FullName COLLATE Latin1_General_100_CI_AS,
                               b.FullName COLLATE Latin1_General_100_CI_AS) AS NameScore
FROM dbo.Customer AS a
JOIN dbo.Customer AS b
  ON a.CustomerID < b.CustomerID AND a.Postcode = b.Postcode -- blocking key: only compare within a postcode WHERE JARO_WINKLER_SIMILARITY(a.FullName COLLATE Latin1_General_100_CI_AS, b.FullName COLLATE Latin1_General_100_CI_AS) >= 90
ORDER BY NameScore DESC;

The a.Postcode = b.Postcode line is the most important part of the query. It’s the blocking key.

The DBA bit: this is a cross join in disguise

Without a blocking key, comparing every row with every other row is an n² problem. 100,000 customers means roughly 5 billion comparisons. No index can help, because the similarity score is calculated for each pair.

There are three ways to keep it under control:

  1. Block first. Only compare rows that share something cheap and indexable: postcode, date of birth, first letter of the surname or a phone-number suffix.
  2. Combine signals. A name score of 88 on its own is weak. A name score of 88 plus the same date of birth is strong. Score several columns and act on the combination.
  3. Run it as a batch job. Fuzzy deduplication belongs in a scheduled data-quality job that writes candidate pairs to a review table. It doesn’t belong in a trigger or in the middle of an OLTP transaction.

Typo-tolerant lookup

For a search box where a user types a name, pre-filter on something indexable and then rank by similarity:


DECLARE @firstName nvarchar(50) = N'Jhon',
        @surname   nvarchar(50) = N'Smiht';

SELECT TOP (10) CustomerID, FirstName, Surname,
       JARO_WINKLER_SIMILARITY(Surname    COLLATE Latin1_General_100_CI_AS,
                               @surname   COLLATE Latin1_General_100_CI_AS) AS SurnameScore,
       JARO_WINKLER_SIMILARITY(FirstName  COLLATE Latin1_General_100_CI_AS,
                               @firstName COLLATE Latin1_General_100_CI_AS) AS FirstNameScore
FROM dbo.Customer
WHERE Surname LIKE LEFT(@surname, 2) + N'%'   -- cheap, SARGable pre-filter on an indexed column
ORDER BY SurnameScore + FirstNameScore DESC;

The pre-filter assumes the first two letters are typed correctly, which is usually true, and Jaro-Winkler leans the same way. If that assumption doesn’t hold for your data, use a different blocking key.

Choosing between the two

  • Jaro-Winkler: names and short strings where the first few characters matter most.
  • Edit distance: codes, product SKUs and addresses, where any single character change matters equally.
  • Thresholds: pick them from your own data. Take a sample of known duplicates and known non-duplicates, score them, and see where the line falls. There’s no universal “90”.

Where it fits with everything else

This rounds out the search toolkit in SQL Server 2025:

  • LIKE for simple patterns
  • Regex for structure and validation
  • Fuzzy matching for “nearly the same”
  • Full-text search for words and word forms
  • Vector search for meaning

Most real systems need more than one of these.

If duplicate customers or suppliers are hurting your reporting, SQL Masters Consulting can help you design a deduplication process that won’t flatten your server.

References

—
Warwick Rudd is a Microsoft Certified Master and Data Platform MVP, and the founder of SQL Masters Consulting. If you’d like to talk about what SQL Server looks like for your environment, get in touch.

Leave a Reply

Your email address will not be published. Required fields are marked *

Warwick Rudd

I am a Microsoft Data Platform MVP as well as a Microsoft Certified Master working as the Principal Consultant here at SQL Masters Consulting. When I am not working with the SQL Server Stack I like to get away to the Snow and spend time Snowboarding.

Search