All Products
Search
Document Center

MaxCompute:FIND_IN_SET

Last Updated:Jul 24, 2023

Returns the position of substring str1 in string str2. The substrings in string str2 are separated by commas (,). The first position is 1.

Syntax

bigint find_in_set(string <str1>, string <str2>)

Parameters

  • str1: required. A value of the STRING type. This parameter specifies the substring whose position you want to obtain.

  • str2: required. A value of the STRING type. This parameter specifies a string in which substrings are separated by commas (,).

Return value

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

  • If str2 does not contain str1 or str1 contains commas (,), 0 is returned.

  • If the value of str1 or str2 is null, the return value is null.

Examples

  • Example 1: Return the position of substring ab in string abc,hello,ab,c. Sample statement:

    -- The return value is 3. 
    select find_in_set('ab', 'abc,hello,ab,c');
  • Example 2: Return the position of substring hi in string abc,hello,ab,c. Sample statement:

    -- The return value is 0. 
    select find_in_set('hi', 'abc,hello,ab,c');
  • Example 3: An input parameter is set to null. Sample statement:

    -- The return value is null. 
    select find_in_set(null, 'abc,hello,ab,c');

Related functions

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