SQL Server DIFFERENCE() Function Example
Let’s look at some examples of the DIFFERENCE function in SQL Server. Learning the DIFFERENCE function with examples will help you understand it better.
Using DIFFERENCE() function with similar SOUNDEX values example
SELECT
SOUNDEX('poor') AS soundex_poor,
SOUNDEX('pour') AS soundex_pour,
DIFFERENCE('poor', 'pour') AS similarity;
Output :
soundex_poor | soundex_pour | similarity |
---|---|---|
P600 | P600 | 4 |
Example 2
In this example, we find difference for highly similar SOUNDEX values
SELECT
SOUNDEX('w3wiki') AS soundex_w3wiki,
SOUNDEX('GeeksOfGeeks') AS soundex_geeksofgeeks,
DIFFERENCE('w3wiki', 'GeeksOfGeeks') AS similarity;
Output :
3
Example 3
In this example, the function returns a DIFFERENCE value of 2, the medium possible difference.
SELECT
SOUNDEX('w3wiki') AS soundex_w3wiki,
SOUNDEX('GFG') AS soundex_GFG,
DIFFERENCE('w3wiki', 'GFG') AS similarity;
Output :
soundex_w3wiki | soundex_GFG | similarity |
---|---|---|
G216 | G120 | 2 |
Example 4
In this example, the function returns a DIFFERENCE value of 0, the highest possible difference.
SELECT
SOUNDEX('javascript') AS soundex_javascript,
SOUNDEX('c#') AS soundex_c#,
DIFFERENCE('javascript', 'c#') AS similarity;
Output :
soundex_javascript | soundex_c# | similarity |
---|---|---|
J126 | C000 | 0 |
SQL Server DIFFERENCE() Function
SQL Server DIFFERENCE() function compares two different SOUNDEX values and returns an integer. This integer value measures the degree to which the SOUNDEX values match, on a scale of 0 to 4.
A value of 0 indicates weak or no similarity between the SOUNDEX values; 4 indicates that the SOUNDEX values are extremely similar, or even identical.