Over a million developers have joined DZone.

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

Learn NoSQL for free with hands-on sample code, example queries, tutorials, and more.  Brought to you in partnership with Couchbase.

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 alike 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’)

  • Result of string 1: Pck

  • Result of string 2: Pk

  • 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

  • Result of string 1: 122

  • Result of string 2: 12

  • 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.

  • 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)

  • Result of string 2: 12

  • Result of string 3: 12

Step 5

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

  • Result of string 1: P2

  • Result of string 2: P2

  • 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.

  • Result of string 1: P200

  • Result of string 2: P200

  • 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.

The Getting Started with NoSQL Guide will get you hands-on with NoSQL in minutes with no coding needed. Brought to you in partnership with Couchbase.

Topics:
pl/sql ,oracle ,soundex ,database

Published at DZone with permission of Paras Shah. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}