

# String similarity functions

Use these functions to measure how alike two strings are, for example to match names that are spelled in slightly different ways.

Distance functions count edits, so a lower value means more alike, and similarity functions return a score from `0` to `1`, where higher means more alike.

## `damerau_levenshtein` function
<a name="athena-duckdb-functions-string-damerau-levenshtein"></a>

Returns the smallest number of single-character edits that change `s1` into `s2`. An edit is an insertion, a deletion, a substitution, or a swap of two adjacent characters. `levenshtein` counts the same swap as two edits.

The comparison is case-sensitive, so `a` and `A` are different characters. If either argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-damerau-levenshtein-syntax"></a>

```sql
damerau_levenshtein({{s1}}, {{s2}})
```

### Arguments
<a name="athena-duckdb-functions-string-damerau-levenshtein-arguments"></a>

The `damerau_levenshtein` function takes the following arguments:

**{{s1}}**  
The first string to compare, as a `VARCHAR` value.

**{{s2}}**  
The second string to compare, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-damerau-levenshtein-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-string-damerau-levenshtein-examples"></a>

The following SQL query returns the distance between two strings that differ by two swaps.

```sql
SELECT damerau_levenshtein('duckdb', 'udckbd') AS result
```

The query returns the following result.


| result | 
| --- | 
| `2` | 

The following SQL query compares one swap with `levenshtein`, changes the case of one letter, and passes `NULL`.

```sql
SELECT
    damerau_levenshtein('ab', 'ba') AS one_swap,
    levenshtein('ab', 'ba') AS levenshtein_swap,
    damerau_levenshtein('Duck', 'duck') AS case_change,
    damerau_levenshtein(NULL, 'abc') AS null_input
```

The query returns the following result.


| one\_swap | levenshtein\_swap | case\_change | null\_input | 
| --- | --- | --- | --- | 
| `1` | `2` | `1` | `NULL` | 

## `hamming` function
<a name="athena-duckdb-functions-string-hamming"></a>

Returns the Hamming distance between `string1` and `string2`: the number of character positions at which the two strings differ. The comparison is case-sensitive. If either argument is `NULL`, the result is `NULL`.

`mismatches` is an alias of `hamming`.

### Syntax
<a name="athena-duckdb-functions-string-hamming-syntax"></a>

```sql
hamming({{string1}}, {{string2}})
```

### Arguments
<a name="athena-duckdb-functions-string-hamming-arguments"></a>

The `hamming` function takes the following arguments:

**{{string1}}**  
The first string to compare, as a `VARCHAR` value.

**{{string2}}**  
The second string to compare, as a `VARCHAR` value. It must have the same length as `string1`.

### Return type
<a name="athena-duckdb-functions-string-hamming-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-string-hamming-examples"></a>

The following SQL query compares two four-letter strings that differ in one position.

```sql
SELECT hamming('duck', 'luck') AS result
```

The query returns the following result.


| result | 
| --- | 
| `1` | 

The following SQL query shows that the comparison is case-sensitive.

```sql
SELECT hamming('Duck', 'duck') AS result
```

The query returns the following result.


| result | 
| --- | 
| `1` | 

The following SQL query uses the `mismatches` alias. The strings `karolin` and `kathrin` differ in three positions.

```sql
SELECT mismatches('karolin', 'kathrin') AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

### Considerations
<a name="athena-duckdb-functions-string-hamming-considerations"></a>

If the two strings have different lengths, or if either string is empty, the query fails. To compare strings of different lengths, use the [`levenshtein` function](#athena-duckdb-functions-string-levenshtein).

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-hamming-differences-from-engine-version-3"></a>

In Athena engine version 3, this function is named `hamming_distance`. DuckDB does not have a `hamming_distance` function. Use `hamming` or its alias `mismatches` instead.

In Athena engine version 3, `hamming_distance('', '')` returns `0`. In DuckDB, `hamming` fails when either string is empty.

## `jaccard` function
<a name="athena-duckdb-functions-string-jaccard"></a>

Returns the Jaccard similarity of `string1` and `string2`: the number of distinct characters that appear in both strings, divided by the number of distinct characters that appear in either string. The result is a `DOUBLE` from `0` (no characters in common) to `1` (the same set of characters).

Because the function compares sets of characters, the order and repetition of characters do not affect the result. The comparison is case-sensitive. If either argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-jaccard-syntax"></a>

```sql
jaccard({{string1}}, {{string2}})
```

### Arguments
<a name="athena-duckdb-functions-string-jaccard-arguments"></a>

The `jaccard` function takes the following arguments:

**{{string1}}**  
The first string to compare, as a `VARCHAR` value.

**{{string2}}**  
The second string to compare, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-jaccard-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-string-jaccard-examples"></a>

The following SQL query compares `duck` and `luck`, which share three of their five distinct characters.

```sql
SELECT jaccard('duck', 'luck') AS result
```

The query returns the following result.


| result | 
| --- | 
| `0.6` | 

The following SQL query compares two strings that have the same characters in a different order.

```sql
SELECT jaccard('abc', 'cba') AS result
```

The query returns the following result.


| result | 
| --- | 
| `1.0` | 

### Considerations
<a name="athena-duckdb-functions-string-jaccard-considerations"></a>

If either string is empty, the query fails.

## `jaro_similarity` function
<a name="athena-duckdb-functions-string-jaro-similarity"></a>

Returns the Jaro similarity of `string1` and `string2`, a `DOUBLE` from `0` (no similarity) to `1` (identical strings). The score is based on how many characters the two strings have in common within a short distance of the same position. It is also based on how many of those characters are out of order. The comparison is case-sensitive.

If you pass `score_cutoff`, the function returns `0` for any similarity lower than the cutoff. If any argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-jaro-similarity-syntax"></a>

```sql
jaro_similarity({{string1}}, {{string2}} [, {{score_cutoff}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-jaro-similarity-arguments"></a>

The `jaro_similarity` function takes the following arguments:

**{{string1}}**  
The first string to compare, as a `VARCHAR` value.

**{{string2}}**  
The second string to compare, as a `VARCHAR` value.

**{{score\_cutoff}}**  
The minimum score, as a `DOUBLE` value. When the similarity is lower than `score_cutoff`, the function returns `0` instead. If you omit `score_cutoff`, the function uses `0`, so it returns every score unchanged. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-jaro-similarity-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-string-jaro-similarity-examples"></a>

The following SQL query compares `duck` and `duckdb`. All four characters of `duck` match, but the two extra characters in `duckdb` keep the similarity less than `1`.

```sql
SELECT jaro_similarity('duck', 'duckdb') AS result
```

The query returns the following result.


| result | 
| --- | 
| `0.8888888888888888` | 

The following SQL query passes a `score_cutoff` of `0.9`. The similarity, about `0.89`, is lower than the cutoff, so the result is `0.0`.

```sql
SELECT jaro_similarity('duck', 'duckdb', 0.9) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0.0` | 

### Considerations
<a name="athena-duckdb-functions-string-jaro-similarity-considerations"></a>

Two empty strings have a similarity of `0`, not `1`.

## `jaro_winkler_similarity` function
<a name="athena-duckdb-functions-string-jaro-winkler-similarity"></a>

Returns the Jaro-Winkler similarity of `string1` and `string2`, a `DOUBLE` from `0` to `1`. The score starts from the Jaro similarity that `jaro_similarity` returns. When that similarity is greater than `0.7`, the score is raised for each character of the prefix that the strings share, up to four characters. Otherwise, the result equals the Jaro similarity. The comparison is case-sensitive.

If you pass `score_cutoff`, the function returns `0` for any similarity lower than the cutoff. If any argument is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-jaro-winkler-similarity-syntax"></a>

```sql
jaro_winkler_similarity({{string1}}, {{string2}}
    [, {{score_cutoff}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-jaro-winkler-similarity-arguments"></a>

The `jaro_winkler_similarity` function takes the following arguments:

**{{string1}}**  
The first string to compare, as a `VARCHAR` value.

**{{string2}}**  
The second string to compare, as a `VARCHAR` value.

**{{score\_cutoff}}**  
The minimum score, as a `DOUBLE` value. When the similarity is lower than `score_cutoff`, the function returns `0` instead. If you omit `score_cutoff`, the function uses `0`, so it returns every score unchanged. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-jaro-winkler-similarity-return-type"></a>

`DOUBLE`

### Examples
<a name="athena-duckdb-functions-string-jaro-winkler-similarity-examples"></a>

The following SQL query compares `duck` and `duckdb`. Because the strings share a four-character prefix, the result is higher than the Jaro similarity of the same strings.

```sql
SELECT
    jaro_winkler_similarity('duck', 'duckdb') AS result
```

The query returns the following result.


| result | 
| --- | 
| `0.9333333333333333` | 

The following SQL query compares the Jaro and Jaro-Winkler similarity of two strings that share the prefix `mar`. The Jaro-Winkler score is higher because of that shared prefix.

```sql
SELECT
    jaro_similarity('martha', 'marhta') AS jaro,
    jaro_winkler_similarity(
        'martha', 'marhta'
    ) AS jaro_winkler
```

The query returns the following result.


| jaro | jaro\_winkler | 
| --- | --- | 
| `0.9444444444444445` | `0.9611111111111111` | 

The following SQL query passes a `score_cutoff` of `0.95`. The similarity, about `0.93`, is lower than the cutoff, so the result is `0.0`.

```sql
SELECT
    jaro_winkler_similarity(
        'duck', 'duckdb', 0.95
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0.0` | 

### Considerations
<a name="athena-duckdb-functions-string-jaro-winkler-similarity-considerations"></a>

Two empty strings have a similarity of `0`, not `1`.

## `levenshtein` function
<a name="athena-duckdb-functions-string-levenshtein"></a>

Returns the Levenshtein distance between `string1` and `string2`. To find it, the function counts the fewest edits that change `string1` into `string2`, where each edit inserts, deletes, or replaces one character. The strings can have different lengths. The comparison is case-sensitive. If either argument is `NULL`, the result is `NULL`.

`editdist3` is an alias of `levenshtein`.

### Syntax
<a name="athena-duckdb-functions-string-levenshtein-syntax"></a>

```sql
levenshtein({{string1}}, {{string2}})
```

### Arguments
<a name="athena-duckdb-functions-string-levenshtein-arguments"></a>

The `levenshtein` function takes the following arguments:

**{{string1}}**  
The first string to compare, as a `VARCHAR` value.

**{{string2}}**  
The second string to compare, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-levenshtein-return-type"></a>

`BIGINT`

### Examples
<a name="athena-duckdb-functions-string-levenshtein-examples"></a>

The following SQL query returns the number of edits that change `duck` into `db`.

```sql
SELECT levenshtein('duck', 'db') AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

The following SQL query shows that the comparison is case-sensitive.

```sql
SELECT levenshtein('Duck', 'duck') AS result
```

The query returns the following result.


| result | 
| --- | 
| `1` | 

The following SQL query uses the `editdist3` alias. Changing `kitten` into `sitting` takes three edits: two replaced characters and one inserted character.

```sql
SELECT editdist3('kitten', 'sitting') AS result
```

The query returns the following result.


| result | 
| --- | 
| `3` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-levenshtein-differences-from-engine-version-3"></a>

In Athena engine version 3, this function is named `levenshtein_distance`. DuckDB does not have a `levenshtein_distance` function. Use `levenshtein` or its alias `editdist3` instead.