Skip to main content

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

ParameterDescription
<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

  1. Remove leading and trailing spaces
SELECT trim_in('   ab d   ') str;
+------+
| str |
+------+
| ab d |
+------+
  1. Remove specified character set
SELECT trim_in('ababccaab', 'ab') str;
+------+
| str |
+------+
| cc |
+------+
  1. Comparison with TRIM function
SELECT trim_in('ababccaab', 'ab'), trim('ababccaab', 'ab');
+-----------------------------+--------------------------+
| trim_in('ababccaab', 'ab') | trim('ababccaab', 'ab') |
+-----------------------------+--------------------------+
| cc | ababccaab |
+-----------------------------+--------------------------+
  1. Character set order does not matter
SELECT trim_in('abcHelloabc', 'cba');
+--------------------------------+
| trim_in('abcHelloabc', 'cba') |
+--------------------------------+
| Hello |
+--------------------------------+
  1. UTF-8 character support
SELECT trim_in('+++ṭṛì ḍḍumai+++', '+');
+--------------------------------------+
| trim_in('+++ṭṛì ḍḍumai+++', '+') |
+--------------------------------------+
| ṭṛì ḍḍumai |
+--------------------------------------+
  1. NULL value handling
SELECT trim_in(NULL, 'abc');
+-----------------------+
| trim_in(NULL, 'abc') |
+-----------------------+
| NULL |
+-----------------------+
  1. Empty character handling
SELECT trim_in('', 'abc'), trim_in('abc', '');
+--------------------+--------------------+
| trim_in('', 'abc') | trim_in('abc', '') |
+--------------------+--------------------+
| | abc |
+--------------------+--------------------+

Keywords

TRIM_IN, TRIM