Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions docs/t-sql/functions/edit-distance-transact-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ monikerRange: "=azuresqldb-current || =azuresqldb-mi-current || =fabric-sqldb ||

[!INCLUDE [preview](../../includes/preview.md)]

Calculates the number of insertions, deletions, substitutions, and transpositions needed to transform one string to another.
Calculates the distance that is the number of insertions, deletions, substitutions, and transpositions needed to transform one string to another.

> [!NOTE]
>
Expand All @@ -47,18 +47,23 @@ An alphanumeric expression of character data. *character_expression* can be a co

#### *maximum_distance*

The maximum distance that should be computed. *maximum_distance* is an integer. If greater than or equal to zero, then the function returns the actual distance value or a distance value that is greater than *maxiumum_distance* value. If the actual distance is greater than *maximum_distance*, then the function might return a value greater than or equal to *maximum_distance*. If the parameter isn't specified or if *maximum_distance* is negative, then the function returns the actual number of transformations needed. If the value is NULL, then the function returns NULL.
The maximum distance that should be computed. *maximum_distance* is an integer. If greater than or equal to zero, then the function stops calculating the distance when the *maximum_distance* is reached.

## Return value

**int**

Returns the distance between the two *character_expressions* using Damerau-Levenshtein algorithm, or *maximum_distance* value if that is smaller.
If any of the inputs is `NULL` then the function returns a `NULL` value.

## Remarks

This function implements the Damerau-Levenshtein algorithm. If any of the inputs is `NULL` then the function returns a `NULL` value. Otherwise, the function returns an integer value from 0 to the number of transformations or *maximum_distance* value.
If the actual distance is greater than *maximum_distance*, then the function might return a value greater than or equal to *maximum_distance*.

## Examples

### Example 1

The following example compares two words and returns the `EDIT_DISTANCE()` value as a column, named `Distance`.

```sql
Expand All @@ -75,6 +80,26 @@ WordUK WordUS Distance
Colour Color 1
```

### Example 2

The following example compares two words and returns the `EDIT_DISTANCE()` limited to a maximum value.

```sql
SELECT Source, Target,
EDIT_DISTANCE(Source, Target) AS ActualDistance,
EDIT_DISTANCE(Source, Target,2) AS LimitedDistance
FROM (VALUES('Chocolate', 'Sweets')) compare(Source, Target) ;

```

Returns:

```output
Source Target ActualDistance LimitedDistance
--------- --------- -------------- ---------------
Chocolate Sweets 8 2
```

For additional examples, see [Example *EDIT_DISTANCE()*](../../relational-databases/fuzzy-string-match/overview.md#example-edit_distance).

## Related content
Expand Down