All Products
Search
Document Center

MaxCompute:TRIM

Last Updated:Aug 21, 2023

Removes the characters from both of the left and right sides of a string.

Usage notes

Currently, the TRIM function supports only English characters.

Syntax

string trim(string <str>[,<trimChars>])
string trim([BOTH] [<trimChars>] from <str>)

Parameters

This function is used to remove the characters from both of the left and right sides of a string that is specified by str.

  • If you do not specify trimChars, the spaces on both of the left and right sides are removed by default.

  • If you specify trimChars, the substrings that consist of one or more characters specified by trimChars are removed from both of the left and right sides of the string that is specified by str.

  • str: required. A value of the STRING type. This parameter specifies the string from both of the left and right sides of which the characters are removed. If the input value is of the BIGINT, DECIMAL, DOUBLE, or DATETIME type, the value is implicitly converted into a value of the STRING type before calculation.

  • trimChars: optional. A value of the STRING type. This parameter specifies the characters to be removed.

Return value

A value of the STRING type is returned. The return value varies based on the following rules:

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

  • If the value of str or trimChars is null, null is returned.

Examples:

  • Example 1: Remove the spaces from both of the left and right sides of the string yxTxyomxx . Sample statement:

    -- The return value is yxTxyomxx. 
    select trim(' yxTxyomxx ');
    -- The preceding statement is equivalent to the following statement: 
    select trim(both from ' yxTxyomxx ');
    select trim(from ' yxTxyomxx ');
  • Example 2: Remove the substrings that consist of one or more characters in the xy collection from both of the left and right sides of the string yxTxyomxx.

    -- The return value is Txyom. Any x or y character on the left or right side is removed. 
    select trim('yxTxyomxx', 'xy');
    -- The preceding statement is equivalent to the following statement: 
    select trim(both 'xy' from 'yxTxyomxx');
    select trim('xy' from 'yxTxyomxx');
  • Example 3: The input parameter is set to null. Sample statement:

    -- The return value is null. 
    select trim(null);
    select trim('yxTxyomxx', null);

Related functions

TRIM is a string function. For more information about functions related to string searches and conversion, see String functions.