The TRANSLATE function performs character-by-character string replacement, converting characters in the source string according to mapping rules. This function replaces each character in the source string that appears in the ‘from’ string with the corresponding character at the same position in the ‘to’ string.
TRANSLATE(<source>, <from>, <to>)
| Parameter | Description |
|---|---|
<source> | The source string to be converted. Type: VARCHAR |
<from> | The set of characters to be replaced. Type: VARCHAR |
<to> | The set of replacement characters. Type: VARCHAR |
Returns VARCHAR type, representing the string transformed according to character mapping rules.
Character mapping rules:
Special cases:
SELECT translate('abcd', 'a', 'z');
+---------------------------+ | translate('abcd', 'a', 'z') | +---------------------------+ | zbcd | +---------------------------+
SELECT translate('abcda', 'a', 'z');
+----------------------------+ | translate('abcda', 'a', 'z') | +----------------------------+ | zbcdz | +----------------------------+
SELECT translate('Palhoça', 'ç', 'c');
+--------------------------------+ | translate('Palhoça', 'ç', 'c') | +--------------------------------+ | Palhoca | +--------------------------------+
SELECT translate('abcd', 'a', '');
+----------------------------+ | translate('abcd', 'a', '') | +----------------------------+ | bcd | +----------------------------+
SELECT TRANSLATE('abacad', 'aac', 'zxy');
+-----------------------------------+ | TRANSLATE('abacad', 'aac', 'zxy') | +-----------------------------------+ | zbzyzd | +-----------------------------------+
SELECT TRANSLATE('abcde', 'ace', 'xy');
+-------------------------------+ | TRANSLATE('abcde', 'ace', 'xy') | +-------------------------------+ | xbyd | +-------------------------------+
SELECT TRANSLATE('ṭṛì ḍḍumai', 'ṭṛ', 'ab');
+-----------------------------------+ | TRANSLATE('ṭṛì ḍḍumai', 'ṭṛ', 'ab') | +-----------------------------------+ | abì ḍḍumai | +-----------------------------------+
SELECT TRANSLATE('a1b2c3', '123', 'xyz');
+----------------------------------+ | TRANSLATE('a1b2c3', '123', 'xyz') | +----------------------------------+ | axbycz | +----------------------------------+
SELECT TRANSLATE('hello@world.com', '@.', '-_');
+--------------------------------------------+ | TRANSLATE('hello@world.com', '@.', '-_') | +--------------------------------------------+ | hello-world_com | +--------------------------------------------+
TRANSLATE