// strings reference

SQL METAPHONE function

Encodes a string by its English pronunciation, producing phonetic keys that separate names better than SOUNDEX.

類別
Strings
Returns
string
In this category
122 of 172

語法

metaphone(string, max_length)
dmetaphone(string)
dmetaphone_alt(string)

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支援

引擎Support備註
SQL Server Not built in No built-in metaphone.
PostgreSQL 是 Requires the fuzzystrmatch extension.
MySQL Not built in No built-in metaphone.
SQLite Not built in No built-in metaphone.
Oracle Not built in No built-in metaphone.

範例

PostgreSQL

SELECT metaphone('knight', 6) AS code;  -- NFT

PostgreSQL

SELECT dmetaphone('Schmidt') AS primary_code,
       dmetaphone_alt('Schmidt') AS alt_code;  -- XMT, SMT

小知識

  • metaphone requires a maximum code length: metaphone('telephone', 6) keeps at most six code characters. Shorter codes group more names together.
  • Metaphone knows English silent letters and digraphs ('PH' encodes as 'F', initial 'KN' as 'N'), where SOUNDEX maps letters mechanically.
  • fuzzystrmatch's soundex, metaphone and dmetaphone do not work well with multibyte encodings such as UTF-8, so accented letters are not mapped sensibly. Transliterate first, for example with unaccent.

Questions about METAPHONE

Is metaphone better than SOUNDEX?

For English names, usually: it models pronunciation instead of letter classes, so fewer unrelated names collide. It is still English-centric.

What is Double Metaphone for?

It returns a primary code (dmetaphone) and an alternate (dmetaphone_alt) so that spellings from different languages, such as Germanic and Slavic forms of a name, can still meet on one of the two.

Why is metaphone not found in my PostgreSQL?

It lives in the fuzzystrmatch extension: CREATE EXTENSION fuzzystrmatch; enables metaphone, dmetaphone, levenshtein, soundex and difference together.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: PostgreSQL.

// fewer lookups

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.