Sometimes we are required to compare strings based on how the string sounds when spoken. For such a task SQL Server provides SOUNDEX and DIFFERENCE function. In this article we will read about both functions.
SOUNDEX
SOUNDEX converts an alphanumeric string to a four-character code that is based on how the string sounds when spoken. The first character of the code is the first character of character_expression, converted to upper case. The second through fourth characters of the code are numbers that represent the letters in the expression. SOUNDEX ignores the vowels(‘A’,’E’,’I’,’O’,’U’) from alphanumeric strings until that are not the first character of the string. If alphanumeric string is small and necessary to generate a four-character code then extra zeroes are added at the end of the string.
Syntax

Example

In the above example we generate SOUNDX code for strings. Now a question arises -- how does SQL Server generate SOUNDEX code for given alpha numeric string? As I described, that first letter of SOUNDEX code is the first letter of the alpha numeric string and the last 3 digits are assigned according this table.
| Number | Represent the Letters |
| 1 | B,F,P,V |
| 2 | C, G, J, K, Q, S, X, Z |
| 3 | D, T |
| 4 | L |
| 5 | M,N |
| 6 | R |
A, E, I, O, U, H, W, and Y letters are ignored from alpha numeric string.
You can find more information about “SOUNDEX system” here. Soundex
For “csharpcorner” we get “C612” code. In this code “C” is for first letter, 6 for “R”, 1 for “P” and 2 for “C”.
DIFFERENCE
The DIFFERENCE function compares two expressions and assigns a value between 0 and 4, with 0 being little to no similarity and 4 representing the same or very similar phrases. This value is derived from the number of characters in the SOUNDEX of each phrase that are the same.
Syntax

Example

Output

In the above example we get the value between 0 to 4 that represents the similarity betwen the SOUNDEX of two words.
Now we have knowledge of soundex but a question arises -- what is the use of soundex or where can we implement it in our project. Generally SOUNDEX is used in a search engine. SOUNDEX is used in FULL-Text search where we want to search similar words.
Let us create a table and insert some data into this.







RakeshPosted Nov 10, 2016, 1:18 PM
Good topic explains. Nice one.......
Anu VPosted Nov 10, 2016, 3:56 AM
Nice article ......................
SubashPosted Nov 9, 2016, 11:36 PM
Very nice i heared this keywords now only nice to know