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
Marketing wants both sides of every lead email plus the first dot-separated field inside its domain.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
| id | email_name | |
|---|---|---|
| 301 | ana@example.com | ana |
| 302 | ben@example.com | ben |
| 303 | chloe@example.com | chloe |
| 304 | dev@example.com | dev |
Part 1 returns the local email name for every displayed lead.
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;Practice this concept
Marketing wants both email parts plus the first dot-separated field inside each domain.
marketingPrefix tables with marketing.table_name.
idemailemail_nameemail_domaindomain_namemarketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
Sign up free to try it on a real business scenario