string

SPLIT_PART

Splits a string on a delimiter and returns the Nth part (1-based). Native to Postgres; MySQL emulates via SUBSTRING_INDEX and SQLite requires application-side splitting.

SPLIT_PART(string, delimiter, field_number)

Parameters / Modifiers

Parameter Purpose
SUBSTRING_INDEX(str, delim, n) MySQL equivalent — returns everything before/after the nth occurrence
n < 0 (Postgres 14+) Count parts from the end
regexp_split_to_array Postgres: split on a regex and return the whole array

Examples

SELECT SPLIT_PART('foo.bar.baz', '.', 2);

Postgres: returns 'bar'

SELECT SPLIT_PART('a,b,c,d', ',', -1);

Postgres 14+: returns 'd' (last part)

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX('foo.bar.baz','.',2),'.',-1);

MySQL idiom to extract the 2nd part

SELECT SPLIT_PART(email, '@', 2) FROM users;

Postgres: extract the domain portion of an email

Dialect notes / Gotcha

SPLIT_PART is Postgres-only — MySQL/SQLite need SUBSTRING_INDEX or expression tricks. Requesting a field number beyond the number of parts returns '' (Postgres), not NULL.

Related functions

← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet