---
title: levenshtein function
description: Learn the syntax of the levenshtein function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/levenshtein
---

# `levenshtein` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Returns the Levenshtein distance between the strings `str1` and `str2`.

## Syntax

```
levenshtein(str1, str2 [, maxDistance])
```

## Arguments

* `str1`: A `STRING` expression.
* `str2`: A `STRING` expression.
* `maxDistance`: An optional `INTEGER` expression limiting the maximum Levenshtein distance considered. The default is unlimited.

  **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 13.3 LTS and above

## Returns

An `INTEGER`.
If `maxDistance` is less than the distance between the two strings the function returns -1.
If `maxDistance` is negative the function returns -1.

## Examples

```sql
> SELECT levenshtein('kitten', 'sitting');
 3

> SELECT levenshtein('kitten', 'sitting', 2);
 -1

> SELECT levenshtein('kitten', 'sitting', 4);
 3
```

## Related functions
