The SQL SOUNDEX one of the String Function, which returns the four digit soundex code of the user specified character expressions. In this article we will show you, How to write SOUNDEX Function in SQL Server with example.
SQL Server SOUNDEX Syntax
The basic syntax of the SQL Server SOUNDEX Function is:
SELECT SOUNDEX(Character_Expression) FROM [Source]
This function convert the characters expression into four digit code based on the string sound. It will place first character from the character_expression as first digit, and remaining are number. In some case, it will add 0’s to the end to complete four digit code.
SQL SOUNDEX Function Example 1
The following examples will help you to understand the Soundex function.
-- SQL Server SOUNDEX Function DECLARE @Str VARCHAR(50) SET @Str = 'SQL Server' SELECT SOUNDEX(@Str) AS SoundText SELECT SOUNDEX('SQL') AS SoundText SELECT SOUNDEX('Sales') AS SoundText SELECT SOUNDEX('Suresh') AS SoundText
SQL SOUNDEX Example 2
In this example,We are going to return the SOUNDEX code of the character expression, along with the difference. I suggest you to refer Difference Function article.
-- SQL Server SOUNDEX Function SELECT SOUNDEX('SQL Server') AS Code1, SOUNDEX('SQL') AS Code11, DIFFERENCE('SQL Server', 'SQL') AS Difference1 SELECT SOUNDEX('SQL') AS Code2, SOUNDEX('Suresh') AS Code22, DIFFERENCE('SQL', 'Suresh') AS Difference2 SELECT SOUNDEX('Suresh') AS Code3, SOUNDEX('Srsh') AS Code33, DIFFERENCE('Suresh', 'Srsh') AS Difference3