Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Understanding Soundex in Oracle PL/SQL

DZone's Guide to

Understanding Soundex in Oracle PL/SQL

If you're browsing your data and want to compare strings based on how they sound, Soundex might be able to help. Here's an in-depth explanation of the algorithm.

· Database Zone ·
Free Resource

MariaDB TX, proven in production and driven by the community, is a complete database solution for any and every enterprise — a modern database for modern applications.

Link: Soundex Function in Oracle

Soundex is one more interesting function of Oracle PL/SQL. You might have gotten the basic idea right from the name.

What Is the Soundex Function?

Soundex returns a character string that sounds similar in English. In other words, the input string and returned strings are phonetically equivalent to each other. For example, “pick”, “peek” and “pic” are spelled differently but are phonetically similar (sounds similar).

Why Use Soundex?

Suppose your table contains various misspelled entries of Delhi city as “Delhi”, “Deli”, “Dehli” and “Delhy”. Your task is to retrieve all records with similar sounding names and correct them. Soundex function can be very useful to you here.

Read why Soundex does not work for numbers:

Syntax

SOUNDEX (string);

Example

select city_name from all_cities where Soundex(city_name) = Soundex('Delhi');


Result

CITY_NAME

---------

Delhi

Deli

Dehli

Delhy


Here's how the algorithm works, according to Oracle:

  • Retain the first letter of the string
  • Remove all other occurrences of the following letters: a, e, h, i, o, u, w, y (or change it to zero ‘0’)
  • Assign digits to the remaining letters (after the first) as follows:
    • b, f, p, v = 1

    • c, g, j, k, q, s, x, z = 2

    • d, t = 3

    • l = 4

    • m, n = 5

    • r = 6

  • If two or more letters with the same number were adjacent in the original name (before step 1) or adjacent except for any intervening h and w, then omit all but the first.
  • Replace the first digit with the letter (as in Step 1).
  • If the string is less than four letters pad ‘0’ on the right. If it is more than 4 letters return only the first four positions.

Soundex by Example

Suppose we have 3 phonetically similar strings, like below:

String 1 = Pick

String 2 = Peek

String 3 = Pic

Let's see how Soundex works for these strings.

Step 1

Retain the first letter of the string:

  • Retained letter of string 1: P

  • Retained letter of string 2: P

  • Retained letter of string 3: P

Step 2

Remove all other occurrences of the following letters: a, e, h, i, o, u, w, y (or change it to zero ‘0’)

  • The result of string 1: Pck

  • The result of string 2: Pk

  • The result of string 3: Pc

Step 3

Assign digits to the remaining letters (after the first) as follows:

b, f, p, v = 1 | c, g, j, k, q, s, x, z = 2 | d, t = 3 | l = 4 | m, n = 5 | r = 6

  • The result of string 1: 122

  • The result of string 2: 12

  • The result of string 3: 12

Step 4

If two or more letters with the same number were adjacent in the original name (before step 1) or adjacent except for any intervening h and w, then omit all but the first.

  • The result of string 1: 12 (as c and k are adjacent to each other and have the same digit, we can remove all but one digit)

  • The result of string 2: 12

  • The result of string 3: 12

Step 5

Replace the first digit with the letter that was retained in Step 1

  • The result of string 1: P2

  • The result of string 2: P2

  • The result of string 3: P2

Step 6

If the string is less than four letters, pad ‘0’ on the right. If it is more than four letters, return only the first four positions.

  • The result of string 1: P200

  • The result of string 2: P200

  • The result of string 3: P200

So, the results of all three strings are identical and using Soundex for any one of them will return all the three strings.

For more information, you can read official Oracle documentation of Soundex function.

MariaDB AX is an open source database for modern analytics: distributed, columnar and easy to use.

Topics:
pl/sql ,oracle ,soundex ,database

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}