Categories:

String & Binary Functions (Matching/Comparison)

EDITDISTANCE

Computes the Levenshtein distance between two input strings. It is the number of single-character insertions, deletions or substitutions needed to convert one string to another.

Note

Unlike some other metrics (e.g. Damerau-Levenshtein distance), character transpositions are not considered.

Syntax

EDITDISTANCE( <expr1> , <expr2> )

Usage Notes

  • EDITDISTANCE can be expensive computation-wise. The execution time of it is proportional to the product of the lengths of the inputs.

    As a result, the length of the inputs is limited to 4096 characters each.

Collation Details

No impact. In languages where the alphabet contains digraphs or trigraphs (such as “Dz” and “Dzs” in Hungarian), each character in each digraph and trigraph is treated as an independent character, not as part of a single multi-character letter.

The result is based solely on the characters in the strings, not on the collation specifications of the strings.

Examples

SELECT s, t, EDITDISTANCE(s, t), EDITDISTANCE(t, s) FROM ed;

----------------+-----------------+--------------------+--------------------+
       s        |        t        | editdistance(s, t) | editdistance(t, s) |
----------------+-----------------+--------------------+--------------------+
                |                 | 0                  | 0                  |
 Gute nacht     | Ich weis nicht  | 8                  | 8                  |
 Ich weiß nicht | Ich wei? nicht  | 1                  | 1                  |
 Ich weiß nicht | Ich weiss nicht | 2                  | 2                  |
 Ich weiß nicht | [NULL]          | [NULL]             | [NULL]             |
 Snowflake      | Oracle          | 7                  | 7                  |
 święta         | swieta          | 2                  | 2                  |
 [NULL]         |                 | [NULL]             | [NULL]             |
 [NULL]         | [NULL]          | [NULL]             | [NULL]             |
----------------+-----------------+--------------------+--------------------+