Part 03 / Topic 5 of 9
String functions
The everyday text toolkit, in one pass over the customers:
SELECT first_name || ' ' || last_name AS full_name,
lower(email) AS email,
length(first_name) AS len,
upper(left(first_name, 1)) AS initial
FROM customers
ORDER BY customer_id
LIMIT 3;
full_name | email | len | initial
------------+-------------------+-----+---------
Asha Verma | [email protected] | 4 | A
Ravi Kumar | [email protected] | 4 | R
Meera Iyer | [email protected] | 5 | M
(3 rows)
More you will reach for weekly:
SELECT trim(' padded ') AS trimmed,
replace('Laptop Pro 14', ' ', '-') AS slugged,
substring('order-2025-0042' FROM 7) AS tail,
split_part('[email protected]', '@', 2) AS domain,
initcap('sql for beginners') AS title_case;
trimmed | slugged | tail | domain | title_case
---------+---------------+-----------+-------------+-------------------
padded | Laptop-Pro-14 | 2025-0042 | example.com | Sql For Beginners
(1 row)
|| concatenates — but remember NULL infects it: 'Hi ' || NULL is NULL. concat('Hi ', NULL) treats NULL as empty text instead; pick the behaviour you actually want.
🧪 Quick quiz — answer from memory, then check.
- What does
'a' || NULLreturn? - Which function pulls
example.comout of an email address?
Show the answers
- NULL —
||propagates NULL.concat()would give'a'. split_part(email, '@', 2).
Done here? The practice set for Part 3 — 7 exercises with solutions — is on the part page.