SPLIT_PART
functionsupportedCore SQL functionsSplits a given string at a specified character and returns the requested part. To return all characters after a specified character, you can use the POSITION and SUBSTR functions. For an example, see Returning substrings for email, phone, and date strings. Tip You can use the search optimization service to improve the performance of queries that call this function. For details, see Search optimization service. See also: SPLIT, STRTOK
Syntax
SPLIT_PART(<string>, <delimiter>, <partNumber>)
Parameters
| Name | Required | Description |
|---|---|---|
string | required | Text to be split into parts. |
delimiter | required | Text representing the delimiter to split by. The entire delimiter string is treated as a single delimiter, even if it contains multiple characters. This behavior differs from STRTOK, which treats each character in the delimiter as a separate delimiter. |
partNumber | required | Requested part of the split, which is 1-based so that the first token is token number 1, not token number 0. If the value is negative, the parts are counted backward from the end of the string. |
Returns
This function returns a value of type VARCHAR. If any argument is NULL, the function returns NULL.
Usage notes
- If the partNumber is out of range, the returned value is an empty string.
- If the string starts or is terminated with the delimiter, the system considers empty space before or after the delimiter, respectively, as a valid part of the split result. For an example, see the Examples section below. This means SPLIT_PART can return empty strings as parts, unlike STRTOK, which never returns empty strings.
- If the partNumber is 0, it is treated as 1. In other words, it gets the first element of the split. To avoid confusion over whether indexes are 1-based or 0-based, Snowflake recommends avoiding the use of 0 as a synonym for 1.
- If the delimiter is an empty string, then after the split, the returned value is the input string (the string isn’t split).
Collation details
The collation specifications of all input arguments must be compatible. This function does not support the following collation specifications: - pi (punctuation-insensitive). - cs-ai (case-sensitive, accent-insensitive).