SQL Tutorial SQL Advanced SQL Database SQL References

MySQL SOUNDEX() Function



The MySQL SOUNDEX() function returns a soundex string from a given string. Two strings that sound almost the same should have identical soundex strings. A standard soundex string is four characters long, but this function returns an arbitrarily long string. The SUBSTRING() function can be used on the result to get a standard soundex string.

All non-alphabetic characters in str are ignored by this function. All international alphabetic characters outside the A-Z range are treated as vowels.

While using SOUNDEX() function, the following limitations should be noted:

  • This function, as currently implemented, is intended to work well with strings that are in the English language only. Strings in other languages may not produce reliable results.
  • This function implements the original Soundex algorithm, not the more popular enhanced version (also described by D. Knuth). The difference is that original version discards vowels first and duplicates second, whereas the enhanced version discards duplicates first and vowels second.
  • This function is not guaranteed to provide consistent results with strings that use multi-byte character sets, including UTF-8.

Syntax

SOUNDEX(str)

Parameters

str Required. Specify a string whose soundex string is to be retrieved.

Return Value

Returns the soundex string from a given string.

Example 1:

The example below shows the usage of SOUNDEX() function.

mysql> SELECT SOUNDEX('Hello');
Result: 'H400'

mysql> SELECT SOUNDEX('Principal');
Result: 'P65214'

mysql> SELECT SOUNDEX('Principle');
Result: 'P65214'

Example 2:

Consider a database table called Sample with the following records:

DataWords
Data1Here
Data2Heir
Data3Smith
Data4Smythe
Data5To
Data6Too
Data7Two

To get the soundex string of all records of Words column, the following query can be used:

SELECT *, SOUNDEX(Words) AS SOUNDEX_Value FROM Sample;

This will produce a result similar to:

DataWordsSOUNDEX_Value
Data1HereH600
Data2HeirH600
Data3SmithS530
Data4SmytheS530
Data5ToT000
Data6TooT000
Data7TwoT000

❮ MySQL Functions