SQL EDITDIST3 function
SQLite's weighted edit distance, provided by the spellfix1 extension. It works on UTF-8 characters, not bytes.
- 類別
- Strings
- Returns
integer- In this category
- 119 of 172
語法
editdist3(str1, str2)
editdist3(str1, str2, lang_id)
editdist3('cost_table')
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Different name or syntax | EDIT_DISTANCE in SQL Server 2025, a preview feature. |
| PostgreSQL | Different name or syntax | levenshtein from fuzzystrmatch. |
| MySQL | Not built in | No built-in edit distance. |
| SQLite | 是 | Only after loading the spellfix1 extension. |
| Oracle | Different name or syntax | UTL_MATCH.EDIT_DISTANCE. |
範例
SQLite
SELECT editdist3('kitten', 'sitting') AS distance; -- 400
SQLite
SELECT name FROM users WHERE editdist3(lower(name), 'jon') <= 200;
小知識
- spellfix1 is not compiled into standard SQLite builds or the sqlite3 shell. Build ext/misc/spellfix.c as a loadable extension and load it; editdist3 then becomes available.
- Default costs are 100 per insertion or deletion and 150 per substitution, so editdist3('kitten', 'sitting') is 400, not 3. Scale thresholds accordingly.
- Custom costs come from a table with columns iLang, cFrom, cTo and iCost, loaded with SELECT editdist3('my_costs'). Rows can price multi-character changes such as 'ss' to 'ß'.
Questions about EDITDIST3
Why is editdist3 not found in my SQLite?
It comes from the spellfix1 extension, which ships as source only. Compile ext/misc/spellfix.c, then load it with .load ./spellfix in the shell or load_extension() from your application.
How does editdist3 relate to Levenshtein distance?
It is a weighted edit distance. With the defaults a substitution costs 150 against 100 for an insertion or deletion, so its ranking can differ from plain Levenshtein; a cost table can set all three to the same value.
Does editdist3 distinguish case?
Yes. Unless the cost table says otherwise, 'A' to 'a' is an ordinary substitution. Fold case with lower() first.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.