SPLIT_PART
Splits source_string based on the specified delimiter and returns the substring specified by start_position and the optional end_position.
Syntax
STRING SPLIT_PART(STRING <source_string>, STRING <delimiter>, BIGINT <start_position>[, BIGINT <end_position>])
-- Standard example.
-- Returns a.
SELECT SPLIT_PART('a,b,c,d', ',', 1);Parameters
source_string— STRING, required. The source string to split. BIGINT, DOUBLE, DECIMAL, and DATETIME values are implicitly converted to STRING before processing.delimiter— STRING constant, required. The delimiter used to splitsource_string. The value can be a single character or a multi-character string.start_position— BIGINT constant, required. The index of the first returned segment, starting from 1. The value must be greater than 0.end_position— BIGINT constant, optional. The index of the last returned segment. The value must be greater than or equal tostart_position. If omitted,end_positiontakes the same value asstart_positionand returns only the segment thatstart_positionspecifies.
Return value
Returns a STRING value. The following rules apply:
Index out of range
If
start_positionis greater than the number of segments after splitting, an empty string is returned.If
end_positionis greater than the number of segments after splitting, all segments fromstart_positionto the end of the string are returned.
Delimiter does not exist
If
delimiterdoes not exist insource_stringandstart_positionis 1, the entiresource_stringis returned.If
source_stringis an empty string, an empty string is returned.
Empty delimiter
The original
source_stringis returned directly.
Type error
An error is returned if
source_stringis not of the STRING, BIGINT, DOUBLE, DECIMAL, or DATETIME type.An error is returned if
delimiteris not a STRING constant.An error is returned if
start_positionorend_positionis not a BIGINT constant.
NULL return value
NULL is returned if any parameter, including
delimiter, is NULL.
Examples
Extract segments by index: use the delimiter
,to split the stringa,b,c,dand return the specified segments.-- Returns a. SELECT SPLIT_PART('a,b,c,d', ',', 1); -- Returns a,b. SELECT SPLIT_PART('a,b,c,d', ',', 1, 2);Index out of range: the value of
start_positionis greater than the actual number of segments after splitting.-- Returns an empty string. SELECT SPLIT_PART('a,b,c,d', ',', 10);Delimiter does not exist:
delimiterdoes not exist insource_string.-- Returns a,b,c,d. SELECT SPLIT_PART('a,b,c,d', ':', 1); -- Returns an empty string. SELECT SPLIT_PART('a,b,c,d', ':', 2);Index out of range: the value of
end_positionis greater than the actual number of segments after splitting.-- Returns b,c,d. SELECT SPLIT_PART('a,b,c,d', ',', 2, 6);Extract the username from an email address.
-- Returns alicexxx. SELECT SPLIT_PART('alicexxx@example.com', '@', 1) AS username;Extract a field by using a multi-character delimiter.
-- Returns banana. SELECT SPLIT_PART('apple||banana||cherry', '||', 2) AS second_item;NULL return value: NULL is returned if any parameter, including
delimiter, is NULL.-- All expressions return NULL. SELECT SPLIT_PART(NULL, ',', 1) AS null_string, SPLIT_PART('a,b,c', NULL, 1) AS null_delimiter, SPLIT_PART('a,b,c', ',', NULL) AS null_part_num;