DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Understanding Soundex in Oracle PL/SQL

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.

Paras Shah user avatar by
Paras Shah
·
Dec. 01, 16 · Tutorial
Like (1)
Save
Tweet
Share
7.97K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Soundex PL/SQL Strings Data Types

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Java Version Are You Running? Let’s Take a Look Under the Hood of the JDK!
  • Cloud-Based Transportation Management System
  • Distributed Stateful Edge Platforms
  • How To Validate Three Common Document Types in Python

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: