SQL DIFFERENCE function
Returns an integer 0 (nothing alike) through 4 (same SOUNDEX code). Cheap phonetic similarity for grouping typos of names.
- カテゴリ
- Strings
- Returns
integer- In this category
- 63 of 172
構文
DIFFERENCE(string1, string2)
エンジンサポート
| エンジン | Support | メモ |
|---|---|---|
| SQL Server | はい | Supported under this name. |
| PostgreSQL | Different name or syntax | difference() in the fuzzystrmatch extension, same 0-4 scale |
| MySQL | Not built in | no DIFFERENCE; compare SOUNDEX codes directly |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Not built in | no DIFFERENCE; UTL_MATCH scores edit distance or Jaro-Winkler |
例
SQL Server
SELECT DIFFERENCE('Smith', 'Smyth') AS sim; -- 4
SQL Server
SELECT DIFFERENCE('Green', 'Hill') AS sim; -- 1 (G650 vs H400)
知っておくと便利なこと
- The score counts how many of the four SOUNDEX characters match: 4 means identical codes, not identical words.
- It inherits every SOUNDEX weakness: the first letter is kept literally, so DIFFERENCE('Catherine', 'Katherine') is only 3, plus English bias and unrelated short names that collide.
- Returns NULL if either input is NULL, and it is a scalar call: for fuzzy joins over big tables it scans, with no index help.
- Other engines: PostgreSQL's fuzzystrmatch extension has difference() on the same scale, plus levenshtein(); MySQL has no DIFFERENCE, so compare SOUNDEX codes directly.
Questions about DIFFERENCE
What does DIFFERENCE = 4 mean?
The two inputs have identical SOUNDEX codes; they sound alike by that algorithm. It says nothing about spelling or meaning.
Can I use DIFFERENCE in a JOIN?
Technically yes, but every pair is scored at runtime with no index support: fine on small sets, painful on large ones. Precompute SOUNDEX codes into indexed columns instead.
What is the MySQL equivalent?
None built in. Compare SOUNDEX(a) = SOUNDEX(b) for the same signal, or implement Levenshtein in a function for distance-based matching.
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.
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.