Categories: Development

The MySQL soundex function; Selecting names

Soundex is the algorithm for indexing names by sound or as pronounced. The MySQL SOUNDEX() function returns a Soundex string from a string supplied.

It can also be used to find same-sounding strings from a table.

The example people table:

| id | first_name |
|----|------------|
|  1 |      Aiden |
|  2 |      Aidan |
|  3 |       Ryan |
|  4 |        Tim |
|  5 |      Ayden |
|  6 |       Matt |
|  7 |      Aydin |

You can see there are several “aidens” but they are spelt different however essentially pronounced the same.

This is where SOUNDEX() can help because using a LIKE query will not.

An example SOUNDEX() query to get all names that sound like “aiden”

SELECT `id`, `first_name` FROM `people` WHERE soundex("aiden") = soundex(first_name);

The result:

| id | first_name |
|----|------------|
|  1 |      Aiden |
|  2 |      Aidan |
|  5 |      Ayden |
|  7 |      Aydin |

SOUNDEX() does its job in finding the values that sound the same as “aiden”.

SELECT `id`, `first_name` FROM `people` WHERE soundex("aidn") = soundex(first_name);

Returns the same results, however using soundex("aid") returns nothing, because it doesn’t sound like Aiden.

Mysql Docs for SOUNDEX()

SQL Fiddle example

Share
Tags: MYSQLWeb Dev

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago