How to use SOUNDEX in Sql Server

06.17.2022

Intro

SQL Server provides the SOUNDEX function to convert a string into a four-character code based on how the string sounds when spoken. In this article, we will learn how to use SOUNDEX in SQL Server.

The Syntax

The basic syntax of a SOUNDEX is as follows:

SELECT SOUNDEX(string_name);
  • string is the string to convert to a soundex code.

Getting Setup

For this, we will be using docker. This is recommended for more than just using SQL Server. To find how to install docker go here: https://docs.docker.com/engine/install/

Now create a file called docker-compose.yml and add the following.

version: "3.9"
services:
  db:
    image: "mcr.microsoft.com/mssql/server"
    ports: 
      - 1433:1433
    environment:
        SA_PASSWORD: "Your_password123"
        ACCEPT_EULA: "Y"

Open a terminal and go to the folder the file is located. Then run the following.

docker-compose up

If you are looking for another good reference, you can check here: https://docs.docker.com/samples/aspnet-mssql-compose/.

A Simple Example

The soundex is a straight forward function. Let's run a few example to see how different code compare.

SELECT soundex('hello') as res;
res
H400

We can compare this score to a similar sounding word, "mellow"

SELECT soundex('hello') as res, soundex('mellow') as res2;
res res2
H400 M400

As expected, we see the starting code is different as the two words sound different when sounding out the word.