SOUNDEX
描述
SOUNDEX 函数用于计算字符串的 Soundex 编码。Soundex 是一种语音算法,用于将英文单词编码为表示其发音的代码,相似发音的单词会有相同的编码。
编码规则:返回由一个大写字母和三位数字组成的4字符代码(如 S530)。
语法
SOUNDEX(<expr>)
参数
| 参数 | 说明 |
|---|---|
<expr> | 需要计算 Soundex 编码的字符串(仅支持 ASCII 字符)。类型:VARCHAR |
返回值
返回 VARCHAR(4) 类型,为字符串的 Soundex 编码。
特殊情况:
- 如果参数为 NULL,返回 NULL
- 如果字符串为空或不含字母,返回空字符串
- 仅处理 ASCII 字母,忽略其他字符
- 非 ASCII 字符会导致函数报错
示例
- 基本用法:单词编码
SELECT soundex('Doris');
+------------------+
| soundex('Doris') |
+------------------+
| D620 |
+------------------+
- 相似发音的单词有相同编码
SELECT soundex('Smith'), soundex('Smyth');
+------------------+------------------+
| soundex('Smith') | soundex('Smyth') |
+------------------+------------------+
| S530 | S530 |
+------------------+------------------+
- 空字符串处理
SELECT soundex('');
+-------------+
| soundex('') |
+-------------+
| |
+-------------+
- 处理 NULL 值
SELECT soundex(NULL);
+---------------+
| soundex(NULL) |
+---------------+
| NULL |
+---------------+
- 空字符串返回空字符串
SELECT soundex('');
+-------------+
| soundex('') |
+-------------+
| |
+-------------+
- 仅包含非字母字符返回空字符串
SELECT soundex('123@*%');
+-------------------+
| soundex('123@*%') |
+-------------------+
| |
+-------------------+
- 忽略非字母字符
SELECT soundex('R@b-e123rt'), soundex('Robert');
+-----------------------+-------------------+
| soundex('R@b-e123rt') | soundex('Robert') |
+-----------------------+-------------------+
| R163 | R163 |
+-----------------------+-------------------+
- 仅包含非 ASCII 字符报错示例
SELECT soundex('你好');
ERROR 1105 (HY000): errCode = 2, detailMessage = Not Supported: Not Supported: soundex only supports ASCII, but got: 你
SELECT soundex('Apache Doris 你好');
+--------------------------------+
| soundex('Apache Doris 你好') |
+--------------------------------+
| A123 |
+--------------------------------+