AppDividend
Latest Code Tutorials

# SQL DIFFERENCE Function Example | Difference() Method

SQL DIFFERENCE() is an inbuilt function that is used for returning the difference between the Soundex values. The Soundex value is four characters long. If two strings sound the same, then their Soundex values will be the same. If two strings sound entirely different, then none of the characters Soundex values will be the same.

## SQL DIFFERENCE Function

SQL DIFFERENCE, one of the String Function, compares two SOUNDEX values and returns the integer. An integer value indicates the match for the two SOUNDEX values, from 0 to 4.  The 0 indicates weak or no similarity between the SOUNDEX values. 4 indicates strong similarity or identically SOUNDEX values.

For example, peace and piece sound the same so that they will have the same Soundex values.

```SELECT SOUNDEX ('piece') AS Piece, SOUNDEX ('Peace') AS Peace;
```

See the output.

 Piece Peace P200 P200

See the following syntax.

```SELECT DIFFERENCE (input_string1, input_string2);
```

### Parameters

1. DIFFERENCE: Used for comparing two Soundex values.
2. Input_string1, input_string_2: Strings whose Soundex values is to be considered.

### Examples

1. STRONG SIMILARITY

### Query

```SELECT SOUNDEX('PIECE') AS PIECE, SOUNDEX('PEACE') AS PEACE, DIFFERENCE ('PIECE', 'PEACE')
AS SIMILARITY;
```

### Output

As the Soundex values of both the strings are the same and as there is a strong similarity between them, so 4 is returned.

### Query

```SELECT SOUNDEX (‘TWO’) AS TWO, SOUNDEX(‘TOO’) AS TOO, DIFFERENCE (‘TWO’, ‘TOO’) AS SIMILARITY;
```

### Output

 TWO TOO SIMILARITY T000 T000 4

## Less Similarity

See the following query.

```SELECT SOUNDEX (‘COFFEE’) AS COFEE, SOUNDEX(‘LAPTOP’) AS LAPTOP, DIFFERENCE (‘COFFEE’, ‘LAPTOP’)
AS SIMILARITY;
```

### Output

 COFFEE LAPTOP SIMILARITY C100 L131 1

In the above query, both the strings, sounds were different, so there was a minimal similarity between two strings. 1 was returned as output because 1 character of Soundex values had a match.

## No Similarity

### Query

```SELECT SOUNDEX('Tree') AS Tree, SOUNDEX('Captivated') AS Captivated, DIFFERENCE ('Tree', 'Captivated')
AS SIMILARITY;
```

### Output

 Tree Captivated SIMILARITY T600 C131 0

In the above output, there is no match between any characters. So, the value returned was 0.

Finally, SQL DIFFERENCE Function Example | Understanding Difference Method is over.

## Recommended Posts

SQL CHARINDEX Function Example

Subqueries In SQL Tutorial

Alias In SQL Tutorial

SQL TOP, LIMIT Or ROWNUM Clause Example Tutorial

What Is SQL Injection

This site uses Akismet to reduce spam. Learn how your comment data is processed.