Cleaning and Reshaping Text in SQL
Real text data needs reshaping. Functions like lower, trim, left, and || build clean, consistent values.
Text rarely arrives in exactly the shape you need. SQL gives you small
building blocks to fix that:
lower()
and
upper()
change case,
trim()
removes stray spaces, and
left()
grabs the first few characters.
The
||
operator glues strings together. Chain these and you can build a clean,
consistent value out of messy parts.
select product_id,
upper(trim(product_name)) as product_name_clean
from products Schema · Garden Shop Table · customers 9 columns · 20 rows
One row per customer. Some customers have no phone on file.
Generate a login handle for each customer: the first letter
of their first name joined to their full
last name, all in lowercase. Return customer_id, first_name, last_name, and the handle as
login. Sort by customer_id.
Run your query to inspect the result.
Expected answer
- Columns: customer_id, first_name, last_name, login.
- Rows: 20 customers.
- Theo Brandt becomes tbrandt; Owen Castellano becomes ocastellano.
Related
Date parts, math, and truncation.
Everyday date and text functions.
Practice safe date filters, buckets, and lead-time reports.
Use a half-open range, not BETWEEN, on timestamps.
Length, case, trimming, slicing, and concatenation.
Debug filters that match nothing.