Lesson 2 of 2 Regular Expressions and Fuzzy Matching

Fuzzy String Matching with Edit Distance and Jaro-Winkler

This lesson explains the edit distance and Jaro-Winkler functions and how to use them to match similar strings at scale.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish between edit distance and Jaro-Winkler measures and their scales.
  • Choose a fuzzy function for codes or for names.
  • Explain how blocking keys make fuzzy matching practical on large tables.

Edit distance

EDIT_DISTANCE(a, b [, max]) returns the minimum number of single-character insertions, deletions, substitutions and transpositions of adjacent characters needed to turn one string into the other, using the Damerau-Levenshtein optimal string alignment algorithm. 'Colour' and 'Color' have a distance of 1. The optional maximum makes the function stop once the distance exceeds it and return the maximum plus one, which saves time when only close matches matter. EDIT_DISTANCE_SIMILARITY(a, b) converts the distance into an integer from 0 (no similarity) to 100 (identical) relative to the string lengths, so a threshold such as 85 works for short and long strings alike.

Fill in the value

Counting edits

Exam skills 1.3.5

Work out edit distances as EDIT_DISTANCE computes them.

Log in to try this activity Interactive activities are free with an account.

Jaro-Winkler

JARO_WINKLER_DISTANCE(a, b) returns a value from 0 (identical) to 1 (completely different), and JARO_WINKLER_SIMILARITY(a, b) returns 0 to 100. The Jaro measure counts matching characters within a window and transpositions, and the Winkler adjustment rewards strings that share a common prefix. That makes it well suited to short strings such as personal and company names, where the beginning is usually typed correctly. The following comparison shows when to use each family.

Edit distance functions
MeasuresNumber of character edits
ScaleEDIT_DISTANCE: edit count; similarity 0-100
Good forCodes, addresses, longer strings with typos
Choose when Counting typos or limiting edits
Jaro-Winkler functions
MeasuresCharacter matches and transpositions, with a prefix bonus
ScaleDistance 0-1 (0 = same); similarity 0-100
Good forShort strings such as names
Choose when Matching names where prefixes matter
Sort into groups

Which fuzzy function?

Exam skills 1.3.5

Choose the fuzzy matching family for each task.

Log in to try this activity Interactive activities are free with an account.

Using fuzzy matching at scale

The fuzzy functions are in preview. In SQL Server 2025 they require ALTER DATABASE SCOPED CONFIGURATION SET PREVIEW_FEATURES = ON; they are also available in Azure SQL Database, SQL database in Fabric and Azure SQL Managed Instance (SQL Server 2025 or Always-up-to-date policy). They support Windows and binary collations but not non-binary SQL_ collations, which can be worked around with a COLLATE clause on the arguments, and inputs cannot be max types.

Comparing every row with every other row grows with the square of the row count, so large matching jobs first block candidates on a cheap exact key, such as postcode, first letter or a phonetic code, and then compute similarity only within each block. Thresholds are tuned on a labelled sample to balance false matches against missed ones.

SELECT a.CustomerID AS IdA, b.CustomerID AS IdB, a.Name, b.Name,
       JARO_WINKLER_SIMILARITY(a.Name, b.Name)  AS NameScore,
       EDIT_DISTANCE(a.Street, b.Street, 3)     AS StreetEdits
FROM Sales.Customer AS a
JOIN Sales.Customer AS b
  ON a.PostalCode = b.PostalCode           -- blocking key
 AND a.CustomerID < b.CustomerID           -- each pair once
WHERE JARO_WINKLER_SIMILARITY(a.Name, b.Name) >= 90
  AND EDIT_DISTANCE(a.Street, b.Street, 3) <= 2;
Scenario questions

Fuzzy matching at scale

Exam skills 1.3.5

Choose the approach for each fuzzy matching situation.

Log in to try this activity Interactive activities are free with an account.
Worked example

Finding duplicate customers

A CRM migration created duplicate customers whose names differ by typos, such as 'Jonathon Smyth' and 'Jonathan Smith', at the same postcode. The table has 3 million rows. The team wants candidate pairs ranked by name similarity. What should the developer do?

Self-join the table on postcode (and a.CustomerID < b.CustomerID), compute JARO_WINKLER_SIMILARITY or EDIT_DISTANCE_SIMILARITY on the names, keep pairs above a threshold and order by the score. Blocking on postcode avoids comparing 3 million rows with each other, and the similarity scores rank candidates for review. Equality or LIKE comparisons miss typos, and SOUNDEX alone is too coarse for ranking.

Check yourself

Which is more similar: a JARO_WINKLER_DISTANCE of 0.02 or of 0.4?

Show answer

0.02. For distance functions, lower values mean more similar strings; 0 means identical.

Exam Tips

Remember for the exam

  • Number of edits: EDIT_DISTANCE; normalized 0-100: EDIT_DISTANCE_SIMILARITY.
  • Names with matching beginnings: Jaro-Winkler.
  • JARO_WINKLER_DISTANCE: lower is more similar; the similarity functions: higher is more similar.
  • SQL Server 2025 needs PREVIEW_FEATURES = ON for the fuzzy functions.