Data Engineering

Formatting Phone Numbers and IDs

SQL Mastery Team
April 15, 2026
5 min read

Welcome to **Day 84**. Today we're making data "Human Readable."

The Mess

Database: `1234567890`

Requirement: `(123) 456-7890`

The Solution: SUBSTRING & CONCAT

You can "Slice" the string into parts and glue them back together with formatting.

SELECT

'(' || SUBSTRING(phone, 1, 3) || ') ' ||

SUBSTRING(phone, 4, 3) || '-' ||

SUBSTRING(phone, 7, 4) AS formatted_phone

FROM users;

The Modern Way: FORMAT()

Some databases support a `FORMAT()` function similar to Python or C#:

-- SQL Server / MySQL have variations

SELECT FORMAT(phone, '(###) ###-####') ...

Formatting IDs

Sometimes شما need to "Pad" an ID with zeros (e.g., turning `123` into `000123`).

In Postgres, use `LPAD`:

SELECT LPAD(id::text, 6, '0') FROM orders;

Your Task for Today

Take a 10-digit number and format it as a social security number or a phone number.

*Day 85: Project—The Automated Data Cleanup Pipeline.*

Ready to put your knowledge into practice?

Join SQL Mastery and learn through interactive exercises.