LEVENSHTEIN_DISTANCE()
All functions > STRING > LEVENSHTEIN_DISTANCE()
Returns the Levenshtein (edit) distance between two strings.
Signatures
Returns: Minimum number of single-character edits (insertions, deletions, substitutions) required to change one string into the other
LEVENSHTEIN_DISTANCE(string1: VARCHAR, string2: VARCHAR) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
string1 | VARCHAR | Yes | First string to compare |
string2 | VARCHAR | Yes | Second string to compare |
Notes
- Also known as edit distance
- Case-sensitive comparison
- Unlike Hamming distance, strings can be different lengths (including empty strings)
- If either argument is NULL, the result is NULL (use
NULL(VARCHAR); bareNULLfails inference) - Useful for fuzzy string matching and spell checking
Examples
FeatureQL
SELECT
f1 := LEVENSHTEIN_DISTANCE('kitten', 'sitting'), -- k->s, e->i, +g
f2 := LEVENSHTEIN_DISTANCE('saturday', 'sunday'), -- Complex transformation
f3 := LEVENSHTEIN_DISTANCE('ABC', 'ABC'), -- Identical strings
f4 := LEVENSHTEIN_DISTANCE('abc', 'ABC'), -- Case-sensitive
f5 := LEVENSHTEIN_DISTANCE('', 'hello'), -- From empty string, insert five characters
f6 := LEVENSHTEIN_DISTANCE(NULL(VARCHAR), 'a') -- NULL yields NULL
;Result
| f1 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT | f5 BIGINT | f6 BIGINT |
|---|---|---|---|---|---|
| 3 | 3 | 0 | 3 | 5 | NULL |
On this page