All Products
Search
Document Center

MaxCompute:SUBSTRING_INDEX

Last Updated:Jul 12, 2023

Truncates the string str to a substring from the first character to the nth delimiter. n is specified by count. If count is set to a positive value, the string is truncated from left to right. If count is set to a negative value, the string is truncated from right to left. This function is an additional function of MaxCompute V2.0.

Syntax

string substring_index(string <str>, string <separator>, int <count>)

Parameters

  • str: required. A value of the STRING type. This parameter specifies the string that you want to truncate.

  • separator: required. A delimiter of the STRING type.

  • count: required. A value of the INT type. This parameter specifies the position of the delimiter.

Return value

A value of the STRING type is returned. If an input parameter is set to null, null is returned.

Examples

  • Example 1: Truncate the string https://www.alibabacloud.com. Sample statement:

    -- The return value is https://www.alibabacloud. 
    select substring_index('https://www.alibabacloud.com', '.', 2);
    -- The return value is alibabacloud.com. 
    select substring_index('https://www.alibabacloud.com', '.', -2);
  • Example 2: An input parameter is set to null. Sample statement:

    -- The return value is null. 
    select substring_index('https://www.alibabacloud.com', null, 2);

Related functions

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