In the life of a data analyst or business user, not all data comes clean. Some customer names are in lowercase, some emails have extra spaces, and some fields mix up everything. Thankfully, SQL provides string functions—small but mighty tools that help fix, format, and filter text data.
Let’s explore these string functions through real-life stories:
1. UPPER() – When Names Need Respect
Story:
Priya pulls a list of customer names for printing membership cards. But half the names look like “john smith” instead of “JOHN SMITH.”
SELECT UPPER(CustomerName) AS NameForCard FROM Customers;
Result: Everyone’s name looks clean and capitalized.
2. LOWER() – For Email Uniformity
Story:
The marketing team is validating email addresses. But some are stored like “ALICE@Email.com” and others as “alice@email.com”.
Fix:
SELECT LOWER(Email) AS NormalizedEmail FROM Subscribers;
Result: All emails match correctly during comparison or deduplication.
3. TRIM() – Cleaning the Messy Edges
Story:
Customer service reports include product names like ” Widget ” (extra spaces!). These don’t match correctly when filtering by product.
Fix:
SELECT TRIM(ProductName) FROM Orders;
Result: Clean strings with no unwanted spaces.
4. SUBSTRING() – Getting the Core
Story:
Your system stores tracking numbers like “SHIP123456”, and you only need the numeric part.
Fix:
SELECT SUBSTRING(TrackingCode, 5, 6) AS TrackingNumber FROM Shipments;
Result: Extracts 123456 from SHIP123456.
5. CHARINDEX() – Finding What Matters
Story:
You want to know if an email field contains “@gmail.com”.
Fix:
SELECT Email
FROM Users
WHERE CHARINDEX('@gmail.com', Email) > 0;
Result: Filters only Gmail users.
6. REPLACE() – Quick Fixes Without Coding
Story:
Your marketing team used “&” in brand names like “Tom & Jerry”, but your print system only accepts “and”.
Fix:
SELECT REPLACE(BrandName, '&', 'and') FROM Brands;
Result: “Tom and Jerry” appears in reports.
7. LEN() – Measuring What You Can’t See
Story:
You need to flag customer feedback that’s too short to be useful.
Fix:
SELECT Feedback
FROM Reviews
WHERE LEN(Feedback) < 20;
Result: You find all the one-word or blank responses.
8. LEFT() and RIGHT() – Quick Slicing
Story:
You want to get area codes from phone numbers like “469-555-1212”.
Fix:
SELECT LEFT(PhoneNumber, 3) AS AreaCode FROM Contacts;
Result: Extracts “469” quickly.
Conclusion:
SQL string functions act like data grooming tools—they clean, format, extract, and fix messy strings, just like brushing and ironing clothes before an important meeting.
With a few characters of SQL, you can:
- Standardize names
- Validate emails
- Clean unwanted spaces
- Extract values for analysis
Leave a Reply