SQL SPLIT_PART

Splits a string on a delimiter and returns just ONE numbered piece — the cleanest way to pull out a specific segment of a consistently-structured value.

What & Why

SPLIT_PART(text, delimiter, n) splits a string wherever the delimiter appears, and returns the nth piece (1-indexed). This handles the "email domain," "username-from-email," and similar delimiter-based extraction patterns more directly than POSITION + SUBSTRING combined.

See How It Works

BUSINESS QUESTION

Marketing wants both sides of every lead email plus the first dot-separated field inside its domain.

idcampaign_idemailcreated_atqualified_atconverted_atlead_scoresourcecountry
3011ana@example.com2024-01-21 09:10:00+002024-01-22 11:00:00+002024-02-02 10:00:00+0086google_adsUS
3021ben@example.com2024-01-24 12:40:00+00NULLNULL52google_adsCA
3032chloe@example.com2024-02-16 08:30:00+002024-02-18 14:20:00+00NULL74emailGB
3043dev@example.com2024-03-20 17:15:00+002024-03-21 09:00:00+002024-04-04 16:00:00+0091linkedinUS
Watch the requested part number change the resultStep 1 of 3
SPLIT_PART(email, '@', 1)
idemailemail_name
301ana@example.comana
302ben@example.comben
303chloe@example.comchloe
304dev@example.comdev

Part 1 returns the local email name for every displayed lead.

EXAMPLE QUERY
SELECT
  id,
  email,
  SPLIT_PART(email, '@', 1) AS email_name,
  LOWER(SPLIT_PART(email, '@', 2)) AS email_domain,
  SPLIT_PART(LOWER(SPLIT_PART(email, '@', 2)), '.', 1) AS domain_name
FROM marketing.leads
WHERE POSITION('@' IN email) > 1
ORDER BY id;
Now You Try

Practice this concept

Marketing wants both email parts plus the first dot-separated field inside each domain.

Available schema
marketing

Prefix tables with marketing.table_name.

idemailemail_nameemail_domaindomain_name
marketing.leads
ColumnType
idinteger
campaign_idinteger
emailtext
created_attimestamp with time zone
qualified_attimestamp with time zone
converted_attimestamp with time zone
lead_scoreinteger
sourcetext
countrytext
archive_statustext
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario