TRIM

2024-06-28 05:30:31  Updated

Purpose

TRIM() removes leading and/or trailing characters from a string.

Syntax

TRIM([{{ LEADING | TRAILING | BOTH }[ trim_character ]|trim_character }FROM] trim_source)

Parameters

Parameter Description
LEADING | TRAILING | BOTH The location of the characters to be removed from the string.
  • LEADING indicates the leading characters. If you specify LEADING, the function removes any leading characters that are equal to the value of trim_character.
  • TRAILING indicates the trailing characters. If you specify TRAILING, the function removes any trailing characters that are equal to the value of trim_character.
  • BOTH indicates both the leading and trailing characters. If you specify BOTH or none of the three options, the function removes the leading and trailing characters that are equal to the value of trim_character.
trim_character The character to be removed. Only a single character can be specified for trim_character. If you do not specify trim_character, the default value is a blank space.
trim_source The string that you want to trim. If you specify only trim_source, the function removes the leading and trailing blank spaces.

Note

trim_character and trim_source can be VARCHAR2 or any data type that can be implicitly converted to VARCHAR2. If the function returns a value of the VARCHAR2 data type, the maximum length of the value is the length of the value of trim_source.

Return type

  • If trim_source is of the CHAR or VARCHAR2 data type, the return type is VARCHAR2.

  • If trim_source is of the NCHAR or NVARCHAR2 data type, the return type is NVARCHAR2.

  • If trim_source is of the CLOB data type, the return type is CLOB.

  • If trim_source or trim_character is NULL, the TRIM() function returns NULL.

Examples

The following example removes the leading and trailing XX from XXOceanBaseXX.

obclient> SELECT TRIM('X' FROM 'XXOceanBaseXX') "TRIM" FROM DUAL;
+-----------+
| TRIM      |
+-----------+
| OceanBase |
+-----------+
1 row in set

Contact Us