SOUNDEX

Return the Soundex phonetic code for a string (one uppercase letter followed by three digits).

Category: stringReturns: VARCHARDialect: PostgreSql

Syntax

SOUNDEX(str)

Description

## Overview Computes the classic English-language Soundex phonetic code for the input string. The result is always a four-character code: one uppercase letter (the first letter of the input) followed by three digits that encode the subsequent consonant pattern. Vowels and certain consonants (H, W, Y) are dropped during encoding, and similar-sounding consonants are mapped to the same digit. Soundex is a classic algorithm for fuzzy name matching. It is very fast and easy to index, but it is coarse and tuned for English phonetics. For non-English names or for production-grade fuzzy matching, supplement Soundex with LEVENSHTEIN, trigram similarity, or a specialised phonetic algorithm such as Double Metaphone. ## Behavior - Returns NULL when the input is NULL. - Returns '0000' for an empty string. - The first character of the result is the first letter of the input (uppercased). Non-letter leading characters are skipped until a letter is found. - The remaining three characters are digits 0-6 encoding the phonetic class of the subsequent consonants. - Output is always exactly 4 characters. - Case is ignored. - Operates on ASCII-oriented English phonetics. Accented Latin letters are stripped of their accents before encoding; non-Latin scripts produce unreliable codes. ## Compatibility - Matches the classic American Soundex algorithm as commonly implemented in SQL engines. - Does not implement newer variants such as Daitch-Mokotoff or NYSIIS.

Parameters

NameTypeDescription
strSpecifies the input string for which the Soundex code is computed. Typically a name or word.

Examples

-- Soundex of a name
SELECT SOUNDEX('Robert');  -- 'R163'
-- Similar-sounding names produce the same code
SELECT SOUNDEX('Rupert');  -- 'R163'
-- Different-sounding name
SELECT SOUNDEX('Smith');  -- 'S530'
-- Single-letter input is padded with zeros
SELECT SOUNDEX('A');  -- 'A000'
-- NULL propagates
SELECT SOUNDEX(CAST(NULL AS VARCHAR));  -- NULL
-- Build a phonetic index column for fuzzy lookup
SELECT customer_id, SOUNDEX(last_name) AS last_name_sdx
FROM retail.customers.profiles;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →