Levenshtein Distance in SQL Server: Edit Distance and String Similarity

Comments 0

Share to social media

Calculating how similar two strings are – rather than whether they are identical – requires an edit distance algorithm. The Levenshtein distance counts the minimum number of single-character insertions, deletions, or substitutions needed to transform one string into another. In SQL Server, the best T-SQL implementation is the approach attributed to ‘Arnold Fribble’, which uses a compact string-based technique rather than a full matrix and performs reasonably for most datasets.

For higher performance on larger datasets, a CLR assembly wrapping .NET’s string distance functions provides the Damerau-Levenshtein variant, which also accounts for transpositions. This article covers both approaches with working code and a practical recommendation on when to use each.

Sometimes you need to know how similar words are, rather than whether they are identical. To get a general measure of similarity is tricky, impossible probably, because similarity is so strongly determined by culture. The Soundex algorithm can come up with some matches but insists that, for example, ‘voluptuousness’ and ‘velvet’ are similar. Family genealogists in Britain would never find a search algorithm that helped them to realise that some branches of the Theobald family spell their surname ‘Tibble’, though Soundex knows they are quite close. To gauge this sort of similarity, you need to know the context of language, pronunciation, culture and semantics. However, we can fall back on more scientific measures based on comparing sequences, though they will never be perfect.

‘Edit distance’ is the obvious way of measuring similarity of sequences of values, and strings are just sequences of characters. How many edits are needed to convert the first string into the second? Edits can be insertions, deletions, replacements or transpositions. Algorithms to do this ‘string-to-string correction problem‘ for any sequences have been current since the Sixties, and have become increasingly refined for a range of sciences such as genome research, Forensics, Dendrochronology, and for predictive text.

The Dynamic approach to finding an edit distance is best done with a matrix. Solutions for this exist for all manner of languages. SQL is handicapped by having no support for matrices. An equivalent relation can be used instead, but it is overkill, and slow. We don’t need all the wonderful things a relation gives us. The fastest dynamic solutions in SQL tend to use Unicode strings as cod matrices since these can be cajoled into use as integer matrixes, but they are painful to use and debug.

Fortunately, third-party CLR functions exist for calculating Damerau-Levenshtein Distance in SQL Server. Levenshtein Distance, developed by Vladimir Levenshtein in 1965, is the algorithm we learn in college for measuring edit-difference. It doesn’t deal perfectly with transpositions because it doesn’t even attempt to detect them: it records one transposition as two edits: an insertion and a deletion. The Damerau-Levenshtein algorithm for Edit Distance solves this.

Here is a simple implementation of the Levenshtein algorithm, using the full matrix. In this version, I’ve restricted the length of string just so as to get a good performance. I doubled performance by removing the NVARCHAR(MAX)s. I is, hopefully, useful just to understand the algorithm, because once all the clever short-cuts are used, it can all get rather opaque. 

Here is the same basic code with the additions to turn it into the Damerau-Levenshtein algorithm. 

The best SQL solution I know of for the Levenshtein algorithm is the one attributed pseudonymously to ‘Arnold Fribble’ (possibly a reference to Arnold Rimmer of Red Dwarf, and his ‘friend’ Mr Flibble.) which is a SQL version of the improved Levenshtein algorithm that dispenses with the full matrix and just uses two vectors instead. It is featured here. There are improved versions around, such as the one here. They take around two thirds of the time to produce their result compared with the full-matrix version.

Can this be done in a relational way? Yes, but without support for matrixes, it is a bit painful, with the full-matrix version taking ten times as long as the string-based version. There is plenty of room for improvement though.

What to use? If I need the performance, and I’m allowed CLR, I use the CLR approach. Otherwise, I still go for the ‘Arnold Fribble’ approach to comparing strings.

You may also be interested in: How to use regex in SQL Server 2025 for pattern-based string matching

It scales reasonably well and is reasonably fast, but I can’t help thinking that with window functions and number table, it would be possible to get an ‘edit distance’ figure  efficiently from a routine that is genuinely relational.

You may also be interested in: Fuzzy string search techniques in SQL Server

FAQs: String Comparisons in SQL: Edit Distance and the Levenshtein algorithm

1. What is Levenshtein distance and how does it work in SQL Server?

Levenshtein distance is the minimum number of single-character edits – insertions, deletions, or substitutions – required to change one string into another. A distance of 0 means the strings are identical; a distance of 3 means three edits are needed. In SQL Server, you calculate it using either a T-SQL implementation (Arnold Fribble’s string-based approach) or a CLR assembly that wraps .NET’s string comparison library. Neither is built into SQL Server natively.

2. What is the fastest way to calculate Levenshtein distance in SQL Server?

For production use with large datasets, the CLR approach is faster – it runs compiled .NET code rather than interpreted T-SQL and supports the Damerau-Levenshtein variant which handles transpositions as well as insertions, deletions, and substitutions. For smaller datasets or environments where CLR is not available, Arnold Fribble’s T-SQL implementation is the best pure-SQL option and performs acceptably for most use cases.

3. What is the difference between Levenshtein and Damerau-Levenshtein distance?

Standard Levenshtein distance counts three edit operations: insertions, deletions, and substitutions. Damerau-Levenshtein adds a fourth: transpositions (swapping two adjacent characters). This makes it more accurate for detecting common typing errors like ‘teh’ instead of ‘the’. For most string similarity tasks in SQL Server, the Damerau-Levenshtein variant is preferable when using CLR, as it catches a broader range of human input errors.

Yes, but Levenshtein distance is typically one component of a broader fuzzy search strategy rather than a standalone solution. For high-volume fuzzy search, combine edit distance filtering with indexed full-text search (FTS) to pre-filter candidates before computing exact distances. Alternatively, the Fuzzy Searches article on Simple Talk covers SQL Server SOUNDEX, DIFFERENCE, and trigram-based approaches that may be more appropriate for phonetic similarity rather than character-level edit distance.

About the author

Phil Factor

See Profile

Phil Factor (real name withheld to protect the guilty), aka Database Mole, has 40 years of experience with database-intensive applications. Despite having once been shouted at by a furious Bill Gates at an exhibition in the early 1980s, he has remained resolutely anonymous throughout his career. See also :