TRIM_IN
Description
The TRIM_IN function is used to remove leading and trailing specified characters from a string. If no character set is specified, it removes leading and trailing spaces by default. When a character set is specified, it removes all specified characters from both ends (regardless of their order in the set).
The key feature of TRIM_IN is that it removes any combination of characters from the specified set, while the TRIM function removes characters based on exact string matching.
Syntax
TRIM_IN(<str>[, <rhs>])
Parameters
| Parameter | Description |
|---|---|
<str> | The string to be processed. Type: VARCHAR |
<rhs> | Optional parameter, the set of characters to be removed. Type: VARCHAR |
Return Value
Returns VARCHAR type, representing the processed string.
Special cases:
- If str is NULL, returns NULL
- If rhs is not specified, removes all leading and trailing spaces
- If rhs is specified, removes all characters from both ends that appear in rhs until encountering characters not in rhs
Examples
- Remove leading and trailing spaces
SELECT trim_in(' ab d ') str;
+------+
| str |
+------+
| ab d |
+------+
- Remove specified character set
SELECT trim_in('ababccaab', 'ab') str;
+------+
| str |
+------+
| cc |
+------+
- Comparison with TRIM function
SELECT trim_in('ababccaab', 'ab'), trim('ababccaab', 'ab');
+-----------------------------+--------------------------+
| trim_in('ababccaab', 'ab') | trim('ababccaab', 'ab') |
+-----------------------------+--------------------------+
| cc | ababccaab |
+-----------------------------+--------------------------+
- Character set order does not matter
SELECT trim_in('abcHelloabc', 'cba');
+--------------------------------+
| trim_in('abcHelloabc', 'cba') |
+--------------------------------+
| Hello |
+--------------------------------+
- UTF-8 character support
SELECT trim_in('+++ṭṛì ḍḍumai+++', '+');
+--------------------------------------+
| trim_in('+++ṭṛì ḍḍumai+++', '+') |
+--------------------------------------+
| ṭṛì ḍḍumai |
+--------------------------------------+
- NULL value handling
SELECT trim_in(NULL, 'abc');
+-----------------------+
| trim_in(NULL, 'abc') |
+-----------------------+
| NULL |
+-----------------------+
- Empty character handling
SELECT trim_in('', 'abc'), trim_in('abc', '');
+--------------------+--------------------+
| trim_in('', 'abc') | trim_in('abc', '') |
+--------------------+--------------------+
| | abc |
+--------------------+--------------------+
Keywords
TRIM_IN, TRIM