SPLIT_PART()
All functions > STRING > SPLIT_PART()
Returns a specific part of a string after splitting by a delimiter.
Signatures
Returns: The specified part after splitting, or NULL if the index is missing
SPLIT_PART(string: VARCHAR, delimiter: VARCHAR, index: BIGINT) → VARCHAR sql
| Parameter | Type | Required | Description |
|---|---|---|---|
string | VARCHAR | Yes | String to split |
delimiter | VARCHAR | Yes | Delimiter to split on |
index | BIGINT | Yes | 1-indexed position of the part to return |
Notes
- Index is 1-based (first part is 1, not 0). A negative index counts from the end
- A missing part is NULL. A part that is present and empty stays ''
- More efficient than SPLIT when only one part is needed
- If
STRING,DELIMITER, orINDEXis NULL the result is NULL (use typed NULLs; bareNULLfails inference)
Examples
FeatureQL
RETURN
-- Get year from date
f1 := SPLIT_PART('2024-01-15', '-', 1),
-- Get month from date
f2 := SPLIT_PART('2024-01-15', '-', 2),
-- Get day from date
f3 := SPLIT_PART('2024-01-15', '-', 3),
-- Get middle element
f4 := SPLIT_PART('apple-banana-cherry', '-', 2),
-- No delimiter found
f5 := SPLIT_PART('single', '-', 1),
-- Out-of-range index is NULL
f6 := SPLIT_PART('2024-01-15', '-', 99),
-- NULL yields NULL
f7 := SPLIT_PART(NULL::VARCHAR, '-', 1)
;Result
| f1 VARCHAR | f2 VARCHAR | f3 VARCHAR | f4 VARCHAR | f5 VARCHAR | f6 VARCHAR | f7 VARCHAR |
|---|---|---|---|---|---|---|
| 2024 | 01 | 15 | banana | single | NULL | NULL |
On this page