Updated on 2026-11-06 GMT+08:00

trim

The trim function is used to remove characters from both the left and right ends of str:

  • If trimChars is not specified, space characters are removed by default.
  • If trimChars is specified, the characters in trimChars are treated as a set. The longest possible substring from both ends of str where all characters are in the trimChars set is removed.

Similar functions:

  • ltrim: The ltrim function removes characters from the left end of str.
  • rtrim: The rtrim function removes characters from the right end of str.

Syntax

trim([,]string ) 

or

trim([BOTH] [] from ) 

Parameters

Parameter

Mandatory

Type

Description

str

Yes

STRING

The string from which to remove characters from both ends. If the input is of type BIGINT, DECIMAL, DOUBLE, or DATETIME, it is implicitly converted to STRING type before evaluation.

trimChars

No

STRING

The characters to be removed.

Return Values

Returns a value of type STRING.

  • If str is not of type STRING, BIGINT, DOUBLE, DECIMAL, or DATETIME, an error is returned.
  • If str or trimChars is NULL, NULL is returned.

Example Code

  • Remove the left and right spaces from the string yxabcxx . The command example is as follows.

    Returns the string yxabcxx.

    select trim(' yxabcxx ');

    Equivalent to the following statement.

    select trim(both from ' yxabcxx ');     
  • Remove the substring from both ends of yxabcxx where all characters are in the set xy.

    Returns abc. Whenever an x or y is encountered at either end, it is removed.

    select trim('xy', 'yxabcxx');

    Equivalent to the following statement.

    select trim(both 'xy' from 'yxabcxx');
  • The input parameter is NULL. The command example is as follows.

    Returns NULL.

    select trim(null);      select trim(null, 'yxabcxx');