HAMMING_DISTANCE()

All functions > STRING > HAMMING_DISTANCE()

Returns the Hamming distance between two strings of equal length.

Signatures

Returns: Number of positions at which the corresponding characters differ

HAMMING_DISTANCE(string1: VARCHAR, string2: VARCHAR) → BIGINT
sql
ParameterTypeRequiredDescription
string1VARCHARYesFirst string to compare
string2VARCHARYesSecond string to compare

Notes

  • Both strings must be of equal length
  • If either argument is NULL, the result is NULL (use NULL(VARCHAR); bare NULL fails inference)
  • If lengths differ, the call fails with an error—strings are not silently truncated or padded
  • Case-sensitive comparison
  • Returns the count of differing positions
  • Useful for comparing binary strings or DNA sequences

Examples

FeatureQL
SELECT
    f1 := HAMMING_DISTANCE('karolin', 'kathrin'), -- 3 characters differ
    f2 := HAMMING_DISTANCE('karolin', 'kerstin'), -- 3 characters differ
    f3 := HAMMING_DISTANCE('1011101', '1001001'), -- 2 bits differ
    f4 := HAMMING_DISTANCE('ABC', 'ABC'), -- Identical strings
    f5 := HAMMING_DISTANCE('abc', 'ABC') -- Case-sensitive
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 BIGINTf5 BIGINT
33203

Last update at: 2026/05/26 17:22:09