Edit

Share via


What is fuzzy string matching?

Applies to: SQL Server 2025 (17.x) Preview Azure SQL Database Azure SQL Managed Instance SQL database in Microsoft Fabric Preview

Use fuzzy, or approximate, string matching to check if two strings are similar, and calculate the difference between two strings. Use this capability to identify strings that may be different because of character corruption. Corruption causes may include spelling errors, transposed characters, missing characters, or abbreviations. Fuzzy string matching uses algorithms to detect similar sounding strings.

Note

  • Fuzzy string matching is currently in preview.
  • SQL Server support for fuzzy string matching introduced in SQL Server 2025 (17.x) Preview.
  • Fuzzy string matching is available in Azure SQL Managed Instance configured with the Always-up-to-date update policy.

Fuzzy functions

Function Description
EDIT_DISTANCE Calculates the number of insertions, deletions, substitutions, and transpositions needed to transform one string to another.
EDIT_DISTANCE_SIMILARITY Calculates a similarity value ranging from 0 (indicating no match) to 100 (indicating full match).
JARO_WINKLER_DISTANCE Calculates the edit distance between two strings giving preference to strings that match from the beginning for a set prefix length.
JARO_WINKLER_SIMILARITY Calculates a similarity value ranging from 0 (indicating no match) to 1 (indicating full match).

Note

Currently, the functions do not adhere to the comparison semantics defined by collation settings, such as case-insensitivity and other collation-specific rules. Once support for collation rules is implemented, the functions' output will reflect these semantics and may change accordingly.

Examples

The following examples demonstrate the fuzzy string matching functions.

Example table

Before you can run example queries, create and populate an example table.

To create and populate the example table, connect to a non-production user database, and run the following script:

-- Step 1: Create the table
CREATE TABLE WordPairs (
    WordID INT IDENTITY(1,1) PRIMARY KEY, -- Auto-incrementing ID
    WordUK NVARCHAR(50), -- UK English word
    WordUS NVARCHAR(50)  -- US English word
);

-- Step 2: Insert the data
INSERT INTO WordPairs (WordUK, WordUS) VALUES
('Colour', 'Color'),
('Flavour', 'Flavor'),
('Centre', 'Center'),
('Theatre', 'Theater'),
('Organise', 'Organize'),
('Analyse', 'Analyze'),
('Catalogue', 'Catalog'),
('Programme', 'Program'),
('Metre', 'Meter'),
('Honour', 'Honor'),
('Neighbour', 'Neighbor'),
('Travelling', 'Traveling'),
('Grey', 'Gray'),
('Defence', 'Defense'),
('Practise', 'Practice'), -- Verb form in UK
('Practice', 'Practice'), -- Noun form in both
('Aluminium', 'Aluminum'),
('Cheque', 'Check'); -- Bank cheque vs. check

Example EDIT_DISTANCE

SELECT WordUK, WordUS, EDIT_DISTANCE(WordUK, WordUS) AS Distance
FROM WordPairs
WHERE EDIT_DISTANCE(WordUK, WordUS) <= 2
ORDER BY Distance ASC;

Returns:

WordUK                         WordUS                         Distance
------------------------------ ------------------------------ -----------
Practice                       Practice                       0
Aluminium                      Aluminum                       1
Honour                         Honor                          1
Neighbour                      Neighbor                       1
Travelling                     Traveling                      1
Grey                           Gray                           1
Defence                        Defense                        1
Practise                       Practice                       1
Colour                         Color                          1
Flavour                        Flavor                         1
Organise                       Organize                       1
Analyse                        Analyze                        1
Catalogue                      Catalog                        2
Programme                      Program                        2
Metre                          Meter                          2
Centre                         Center                         2
Theatre                        Theater                        2

Example EDIT_DISTANCE_SIMILARITY

SELECT WordUK, WordUS, EDIT_DISTANCE_SIMILARITY(WordUK, WordUS) AS Similarity
FROM WordPairs
WHERE EDIT_DISTANCE_SIMILARITY(WordUK, WordUS) >=75
ORDER BY Similarity DESC;

Returns:

WordUK                         WordUS                         Similarity
------------------------------ ------------------------------ -----------
Practice                       Practice                       100
Travelling                     Traveling                      90
Aluminium                      Aluminum                       89
Neighbour                      Neighbor                       89
Organise                       Organize                       88
Practise                       Practice                       88
Defence                        Defense                        86
Analyse                        Analyze                        86
Flavour                        Flavor                         86
Colour                         Color                          83
Honour                         Honor                          83
Catalogue                      Catalog                        78
Programme                      Program                        78
Grey                           Gray                           75

Example JARO_WINKLER_DISTANCE

SELECT WordUK, WordUS, JARO_WINKLER_DISTANCE(WordUK, WordUS) AS Distance
FROM WordPairs
WHERE JARO_WINKLER_DISTANCE(WordUK, WordUS) <= .05
ORDER BY Distance ASC;

Returns:

WordUK                         WordUS                         Distance
------------------------------ ------------------------------ -----------
Practice                       Practice                       0
Travelling                     Traveling                      0.02
Neighbour                      Neighbor                       0.0222222222222223
Aluminium                      Aluminum                       0.0222222222222223
Theatre                        Theater                        0.0285714285714286
Flavour                        Flavor                         0.0285714285714286
Centre                         Center                         0.0333333333333333
Colour                         Color                          0.0333333333333333
Honour                         Honor                          0.0333333333333333
Catalogue                      Catalog                        0.0444444444444444
Programme                      Program                        0.0444444444444444
Metre                          Meter                          0.0466666666666667

Example JARO_WINKLER_SIMILARITY

SELECT WordUK, WordUS, JARO_WINKLER_SIMILARITY(WordUK, WordUS) AS Similarity
FROM WordPairs
WHERE JARO_WINKLER_SIMILARITY(WordUK, WordUS) > 90
ORDER BY  Similarity DESC;

Returns:

WordUK                         WordUS                         Similarity
------------------------------ ------------------------------ -----------
Practice                       Practice                       100
Aluminium                      Aluminum                       98
Neighbour                      Neighbor                       98
Travelling                     Traveling                      98
Colour                         Color                          97
Flavour                        Flavor                         97
Centre                         Center                         97
Theatre                        Theater                        97
Honour                         Honor                          97
Catalogue                      Catalog                        96
Programme                      Program                        96
Metre                          Meter                          95
Organise                       Organize                       95
Practise                       Practice                       95
Analyse                        Analyze                        94
Defence                        Defense                        94

Example query with all functions

The following query demonstrates all of the regular expression functions currently available.

SELECT	T.source_string,
		T.target_string,
		EDIT_DISTANCE(T.source_string, T.target_string) as ED_Distance,
		JARO_WINKLER_DISTANCE(T.source_string, T.target_string) as JW_Distance,

		EDIT_DISTANCE_SIMILARITY(T.source_string, T.target_string) as ED_Similarity,
		JARO_WINKLER_SIMILARITY(T.source_string, T.target_string) as JW_Similarity
FROM (VALUES('Black', 'Red'),
			('Colour', 'Yellow'),
			('Colour', 'Color'),
			('Microsoft', 'Msft'),
			('Regex', 'Regex')) as T(source_string, target_string);

Returns:

source_string  target_string  ED_Distance    JW_Distance           ED_Similarity  JW_Similarity
-------------- -------------- -------------- --------------------- -------------- -------------- 
Black	        Red	            5	           1	                   0	        0
Colour	        Yellow	        5	           0.444444444444445 	   17	        55
Colour	        Color	        1	           0.0333333333333333 	   83	        96
Microsoft	    Msft	        5	           0.491666666666667 	   44	        50
Regex	        Regex	        0	           0	                   100	        100

Clean up

After you are done using the example data, delete the example table.

IF OBJECT_ID('dbo.WordPairs', 'U') IS NOT NULL
BEGIN
    DROP TABLE dbo.WordPairs;
END