SQL DIFFERENCE() is one of the String Functions, compares two SOUNDEX values, and returns the integer. An integer value indicates the match for the two SOUNDEX values, 0 to 4. The 0 shows weak or no similarity between the SOUNDEX values. The 4 indicates strong similarity or identically SOUNDEX values.
SQL DIFFERENCE() is a built-in function used to return the difference between the Soundex values. The Soundex value is four characters long. If two strings sound the same, their Soundex values will be the same. However, if two strings sound entirely different, none of the character’s Soundex values will be the same.
For example, peace and piece sound the same to have the same Soundex values.
SELECT SOUNDEX ('piece') AS Piece, SOUNDEX ('Peace') AS Peace;
See the output.
See the following syntax.
SELECT DIFFERENCE (input_string1, input_string2);
- DIFFERENCE: Used for comparing two Soundex values.
- Input_string1, input_string_2: Strings whose Soundex values are to be considered.
- STRONG SIMILARITY
SELECT SOUNDEX('PIECE') AS PIECE, SOUNDEX('PEACE') AS PEACE, DIFFERENCE ('PIECE', 'PEACE') AS SIMILARITY;
As the Soundex values of both the strings are the same and there is a strong similarity between them, 4 is returned.
SELECT SOUNDEX (‘TWO’) AS TWO, SOUNDEX(‘TOO’) AS TOO, DIFFERENCE (‘TWO’, ‘TOO’) AS SIMILARITY;
See the following query.
SELECT SOUNDEX (‘COFFEE’) AS COFEE, SOUNDEX(‘LAPTOP’) AS LAPTOP, DIFFERENCE (‘COFFEE’, ‘LAPTOP’) AS SIMILARITY;
In the above query, both the strings, sounds were different, so there was a minimal similarity between the two strings. Therefore, 1 was returned as output because 1 character of Soundex values had a match.
SELECT SOUNDEX('Tree') AS Tree, SOUNDEX('Captivated') AS Captivated, DIFFERENCE ('Tree', 'Captivated') AS SIMILARITY;
In the above output, there is no match between any characters. So, the value returned was 0.
That’s it for this tutorial.