The TRIM function is used to remove consecutive spaces or specified strings from both ends of a string. If the second parameter is not specified, leading and trailing spaces are removed; if specified, the function removes the specified complete string from both ends.
TRIM(<str>[, <rhs>])
| Parameter | Description |
|---|---|
<str> | The string to be processed. Type: VARCHAR |
<rhs> | Optional parameter, characters to be trimmed from both ends. Type: VARCHAR |
Returns a value of type VARCHAR.
Special cases:
SELECT trim(' ab d ') str;
+------+ | str | +------+ | ab d | +------+
trim(str, rhs) repeatedly strips <rhs> from both the leading and trailing ends of <str> until neither end starts/ends with <rhs>.SELECT trim('ababccaab', 'ab') str;
+------+ | str | +------+ | cca | +------+
SELECT trim(' ṭṛì ḍḍumai ');
+------------------------------+ | trim(' ṭṛì ḍḍumai ') | +------------------------------+ | ṭṛì ḍḍumai | +------------------------------+
SELECT trim('Hello World', 'xyz');
+--------------------------------+ | trim('Hello World', 'xyz') | +--------------------------------+ | Hello World | +--------------------------------+
SELECT trim(NULL), trim('Hello', NULL);
+------------+-----------------------+ | trim(NULL) | trim('Hello', NULL) | +------------+-----------------------+ | NULL | NULL | +------------+-----------------------+
SELECT trim(''), trim('abc', '');
+----------+------------------+ | trim('') | trim('abc', '') | +----------+------------------+ | | abc | +----------+------------------+
SELECT trim('abcabcabc', 'abc');
+------------------------------+ | trim('abcabcabc', 'abc') | +------------------------------+ | | +------------------------------+
SELECT trim('abcHelloabc', 'abc');
+--------------------------------+ | trim('abcHelloabc', 'abc') | +--------------------------------+ | Hello | +--------------------------------+
ṭṛì is stripped because the trailing end does not match the pattern (+++), so trim stops there.SELECT trim('ṭṛì ḍḍumai+++', 'ṭṛì');
+--------------------------------------------+ | trim('ṭṛì ḍḍumai+++', 'ṭṛì') | +--------------------------------------------+ | ḍḍumai+++ | +--------------------------------------------+
TRIM