BigQuery · String function
BigQuery SOUNDEX
SOUNDEX(value) returns a four-character phonetic code (a letter and three digits) for how a word sounds in English: SOUNDEX('Smith') and SOUNDEX('Smyth') both return S530. Use it to find or match names with spelling variations.
SOUNDEX implements the classic Soundex algorithm. It keeps the first letter, encodes the following consonants as digits by sound group, drops vowels and repeated codes, and pads or cuts the result to one letter plus three digits. Names that sound alike in English, such as Robert and Rupert, or Levine and Lavine, get the same code.
Non-Latin characters are ignored, so names in Cyrillic, Greek or CJK scripts return an empty string, and so does an empty input. NULL returns NULL. The whole input produces one code: for a full name, SOUNDEX('John Smith') encodes it as a single word, so split first and last names before comparing.
Soundex is coarse. Different names can share a code and similar names can differ, especially when the first letter differs (Catherine vs Katherine). Use it to generate candidate matches and then rank them, for example with EDIT_DISTANCE, which BigQuery also provides. BigQuery has no DIFFERENCE function like SQL Server.
Syntax
SOUNDEX(value)Arguments and return value
| Argument | Description |
|---|---|
value | The STRING to encode. |
Returns: STRING: a letter followed by three digits, or an empty string if value has no Latin letters. NULL if value is NULL.
Examples
Names that sound alike
Short names are padded with zeros.
SELECT
SOUNDEX('Robert') AS robert,
SOUNDEX('Rupert') AS rupert,
SOUNDEX('Smith') AS smith,
SOUNDEX('Smyth') AS smyth,
SOUNDEX('Lee') AS short_name;| robert | rupert | smith | smyth | short_name |
|---|---|---|---|---|
| R163 | R163 | S530 | S530 | L000 |
Empty, non-Latin and multi-word input
SELECT
SOUNDEX('') AS empty_input,
SOUNDEX('Иван') AS cyrillic,
SOUNDEX('John Smith') AS full_name,
SOUNDEX(CAST(NULL AS STRING)) AS null_input;| empty_input | cyrillic | full_name | null_input |
|---|---|---|---|
| J525 | NULL |
Find possible duplicate customers
WITH customers AS (
SELECT 1 AS customer_id, 'Levine' AS last_name UNION ALL
SELECT 2, 'Lavine' UNION ALL
SELECT 3, 'Lewis'
)
SELECT a.customer_id AS id_a, b.customer_id AS id_b, a.last_name AS name_a, b.last_name AS name_b
FROM customers AS a
JOIN customers AS b
ON SOUNDEX(a.last_name) = SOUNDEX(b.last_name) AND a.customer_id < b.customer_id
ORDER BY id_a, id_b;| id_a | id_b | name_a | name_b |
|---|---|---|---|
| 1 | 2 | Levine | Lavine |
Common mistakes
- Passing a full name. SOUNDEX encodes the whole string as one word; compare first and last names separately.
- Using it for non-English or non-Latin names. Non-Latin characters are ignored, so they return an empty string, and empty strings match each other in a join. Filter out
SOUNDEX(name) = ''before joining. - Treating a code match as a duplicate. Soundex has many false positives and misses names that start with different letters; confirm candidates with EDIT_DISTANCE or other fields.
- Looking for SQL Server's DIFFERENCE. It doesn't exist in BigQuery; compare SOUNDEX codes directly or use EDIT_DISTANCE.
Cost and performance
- Joining on
SOUNDEX(a.name) = SOUNDEX(b.name)groups many rows under the same code, so a self-join can produce a very large number of pairs. Add a blocking condition such as the same country or postcode to keep the join small.
FAQ
How do I do fuzzy name matching in BigQuery?
Use SOUNDEX to find names that sound alike and EDIT_DISTANCE to measure how many characters differ. A common pattern is to join on equal SOUNDEX codes and keep pairs with a small EDIT_DISTANCE.
Why does SOUNDEX return an empty string?
The input is empty or contains no Latin letters. SOUNDEX ignores characters outside the Latin alphabet.
Does BigQuery have a DIFFERENCE function like SQL Server?
No. Compare SOUNDEX(a) = SOUNDEX(b), or use EDIT_DISTANCE for a numeric similarity measure.
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
