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
ParameterTypeRequiredDescription
string1VARCHARYesFirst string to compare
string2VARCHARYesSecond 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); bare NULL fails inference)
  • Useful for fuzzy string matching and spell checking

Examples

FeatureQL
SELECT
    -- k->s, e->i, +g
    f1 := LEVENSHTEIN_DISTANCE('kitten', 'sitting'),
    -- Complex transformation
    f2 := LEVENSHTEIN_DISTANCE('saturday', 'sunday'),
    -- Identical strings
    f3 := LEVENSHTEIN_DISTANCE('ABC', 'ABC'),
    -- Case-sensitive
    f4 := LEVENSHTEIN_DISTANCE('abc', 'ABC'),
    -- From empty string, insert five characters
    f5 := LEVENSHTEIN_DISTANCE('', 'hello'),
    -- NULL yields NULL
    f6 := LEVENSHTEIN_DISTANCE(NULL(VARCHAR), 'a')
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 BIGINTf5 BIGINTf6 BIGINT
33035NULL

Last update at: 2026/06/20 10:08:10