HAMMING_DISTANCE
::: note Since 4.1.2 :::
Description
The HAMMING_DISTANCE function returns the number of positions at which two strings differ.
This function compares strings by UTF-8 characters rather than bytes. The two input strings must have the same number of UTF-8 characters. The comparison is case-sensitive.
Syntax
HAMMING_DISTANCE(<str1>, <str2>)
Parameters
| Parameter | Description |
|---|---|
<str1> | The first string to compare. |
<str2> | The second string to compare. |
Return Value
Returns a BIGINT value representing the number of differing character positions.
Special cases:
- If either argument is NULL, returns NULL.
- If both arguments are empty strings, returns 0.
- If the two strings have different UTF-8 character lengths, an error is returned.
Examples
- Compare two ASCII strings of the same length.
SELECT hamming_distance('karolin', 'kathrin') AS distance;
+----------+
| distance |
+----------+
| 3 |
+----------+
- Compare two UTF-8 strings. The strings have the same character length, and only one character is different.
SELECT hamming_distance('数据库', '数据仓') AS distance;
+----------+
| distance |
+----------+
| 1 |
+----------+
- Compare strings that differ only by letter case.
SELECT hamming_distance('Doris', 'doris') AS distance;
+----------+
| distance |
+----------+
| 1 |
+----------+
- NULL input returns NULL.
SELECT hamming_distance(NULL, 'abc') AS distance;
+----------+
| distance |
+----------+
| NULL |
+----------+