In Split, we saw how to split each string element in a table column into parts around a given string delimiter pattern. We will see in this section how to select one, all, or some of the parts resulting from splitting a string.
This section is organized as follows:
This section is complemented by:
We wish to select any one part from the parts obtained by splitting a string given its index.
In this example, we wish to split each element in the column col_1
around the hyphen ‘-’
character and return the second part.
SELECT col_1,
SPLIT(col_1, '-')[SAFE_OFFSET(1)] AS col_2
FROM table_1;
Here is how this works:
SPLIT()
to return an array of parts for each element in col_1
.
See Split.[SAFE_OFFSET(1)]
to select the second element of the array returned by SPLIT()
.Extension: Select Last Part
In this example, we wish to split each element in the column col_1
around the hyphen ‘-’
character and return the last part.
SELECT col_1,
SPLIT(col_1, '-')[SAFE_OFFSET(ARRAY_LENGTH(SPLIT(col_1, '-')) - 1)] AS col_2
FROM table_1;
Here is how this works:
SAFE_OFFSET()
function.
See Working with Arrays.We wish to select all parts obtained from splitting a string.
In this example, we wish to split each element of the column col_1
around the hyphen character ‘-’
and store each part in a new column. We expect three parts, and we wish to call the three resulting
columns 'col_2'
, 'col_3'
, and 'col_4'
.
SELECT col_1,
SPLIT(col_1, '-')[SAFE_OFFSET(0)] AS col_2,
SPLIT(col_1, '-')[SAFE_OFFSET(1)] AS col_3,
SPLIT(col_1, '-')[SAFE_OFFSET(2)] AS col_4
FROM table_1;
Here is how this works:
We wish to select some parts returned by splitting a string.
In this example, we wish to select parts 1 and 3 resulting from splitting each element of the
column col_1
around the hyphen character ‘-’
as new columns.
SELECT col_1,
SPLIT(col_1, '-')[SAFE_OFFSET(0)] AS col_2,
SPLIT(col_1, '-')[SAFE_OFFSET(2)] AS col_3
FROM table_1;
Here is how this works: