SQL SOUNDEX function
Maps a string to a four-character phonetic code so similarly pronounced names compare equal. Built for English surnames in the early 1900s, and still shipped by most engines.
- Kategorie
- Strings
- Returns
string- In this category
- 62 of 172
Syntax
SOUNDEX(string)
Engine-Unterstützung
| Engine | Support | Notizen |
|---|---|---|
| SQL Server | Ja | Supported under this name. |
| PostgreSQL | Different name or syntax | via the fuzzystrmatch extension |
| MySQL | Ja | codes for long names run past four characters |
| SQLite | Not built in | only when compiled with SQLITE_SOUNDEX (usually off) |
| Oracle | Ja | Supported under this name. |
Beispiele
MySQL
SELECT SOUNDEX('Robert') AS r1, SOUNDEX('Rupert') AS r2;
-- R163, R163
SQL Server
SELECT SOUNDEX('Smith') AS s1, SOUNDEX('Smyth') AS s2;
-- S530, S530
Gut zu wissen
- The algorithm is English-centric: it codes consonant sounds after the first letter and collapses repeats. Non-English names produce unreliable codes.
- Codes pad or truncate to one letter plus three digits, so Smith and Smyth match (S530) but also unrelated short names collide. MySQL does not truncate: SOUNDEX('Quadratically') is Q36324, so compare LEFT(SOUNDEX(x), 4) against codes from other systems.
- PostgreSQL needs CREATE EXTENSION fuzzystrmatch; SQLite ships the function only when built with SQLITE_SOUNDEX, which most builds skip.
- For serious fuzzy matching, levenshtein distances or trigram indexes (pg_trgm) outperform SOUNDEX on precision and recall alike.
Questions about SOUNDEX
When do two names share a SOUNDEX code?
When their consonant sounds map to the same digits after the first letter: Smith/Smyth (S530), Robert/Rupert (R163). Spelling differences that change sounds do change the code.
Is SOUNDEX good for name deduplication?
As a cheap first pass, yes; as the only check, no. Its false positives and English bias are well known. Combine with a Levenshtein threshold or trigram similarity.
Why is SOUNDEX missing in my PostgreSQL install?
It lives in the fuzzystrmatch extension: CREATE EXTENSION fuzzystrmatch; then soundex('Robert') works.
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, MySQL, 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.