DIFFERENCE and SOUNDEX in Transact-SQL

  • 2

DIFFERENCE and SOUNDEX in Transact-SQL

visit: http://www.zonixsoft.com (our official Website)

DIFFERENCE(string1, string2)

Returns the difference between two strings as a value between 0 and 4. This function uses the SOUNDEX algorithm to compare sounds, rather than characters. If the two strings match phonetically very well, the DIFFERENCE() function will return the value 4. If they don’t resemble each other even remotely, it will return the value 0.

The DIFFERENCE function is commonly used to search with names. Instead of a comparison like:

WHERE Authors.AuthorName = sAuthor

where sAuthor is the name supplied by the user, you can use the following comparison:

WHERE DIFFERENCE(Authors.AuthorName, sAuthor) = 4

The latter comparison will match “Mansfield” with “Mansfeeld”, “Petroutsos” with “Petrutsos”, and so on.

SOUNDEX(string)

Returns the four-character SOUNDEX code of a word. You can use this value to compare the closeness of two words. The SOUNDEX function converts a string to a four-character code: the first character of the code is the first character of the string argument, and the second through fourth characters of the code are numbers that code the consonants in the word (vowels are ignored unless they are the first letter of the string). The strings “Brannon”, “Branon”, and “Bramon” all have a SOUNDEX value of B655, because the sounds of n and m are very close. The word “Bradon”, however, has a SOUNDEX value of B635 (it differs by a consonant in the middle of the word).The word “Branok” has a SOUNDEX value of B652, since it differs in a consonant at the end.

 

                                 SOUNDEX (Transact-SQL)

 

 

Definition:

 

Returns a four-character (SOUNDEX) code to evaluate the similarity of two strings.

 

Syntax :

 

SOUNDEX ( character_expression )

 

Arguments :

 

character_expression – Is an alphanumeric expression of character data. character_expression can be a constant, variable, or column.

 

Return Types :

varchar

 

Remarks :

SOUNDEX converts an alphanumeric string to a four-character code to find similar-sounding words or names. The first character of the code is the first character of character_expression and the second through fourth characters of the code are numbers. Vowels in character_expression are ignored unless they are the first letter of the string. String functions can be nested.

 

Examples :

The following example shows the SOUNDEX function and the related DIFFERENCE function. In the first example, the standard SOUNDEX values are returned for all consonants. Returning the SOUNDEX for Smith and Smythe returns the same SOUNDEX result because all vowels, the letter y, doubled letters, and the letter h, are not included.

 

-> Using SOUNDEX

SELECT SOUNDEX (‘Smith’), SOUNDEX (‘Smythe’);

 

Here is the result set.

 

No column name     No column name

S530                           S530

 

(1 row(s) affected)

 

The DIFFERENCE function compares the difference of the SOUNDEX pattern results. The following example shows two strings that differ only in vowels. The difference returned is 4, the lowest possible difference.

 

-> Using DIFFERENCE

SELECT DIFFERENCE(‘Smithers’, ‘Smythers’);

GO

Here is the result set.

 

No column name

4

 

(1 row(s) affected)

 

In the following example, the strings differ in consonants; therefore, the difference returned is 2, the greater difference.

 

SELECT DIFFERENCE(‘Anothers’, ‘Brothers’);

GO

Here is the result set.

 

No column name

2

 

(1 row(s) affected)

 

 


Bookmark and Share