String Functions
UPPER, LOWER, CONCAT, TRIM, LEN, SUBSTRING
✅ What You Will Learn
String functions manipulate text data. In real analytics work, raw data is often inconsistent — names in different cases, extra spaces in addresses, email addresses mixed with phone numbers. String functions let you clean and transform text data directly in SQL.
📋 customers table — string functions operate on name, city, email columns
| customer_id | customer_name | city | |
|---|---|---|---|
| 1 | rahul sharma | Delhi | rahul@gmail.com |
| 2 | PRIYA VERMA | Noida | priya@yahoo.com |
| 3 | Amit Kumar | Gurgaon | amit@gmail.com |
| 4 | Sneha Kapoor | Delhi | sneha@outlook.com |
| 5 | vikram singh | Noida | vikram@gmail.com |
Syntax
Examples
📌 Key Points to Remember
- ✓UPPER and LOWER are useful for standardising case before comparisons
- ✓TRIM removes invisible spaces that cause mismatches
- ✓CONCAT joins multiple text values into one
- ✓LIKE with % is the most common pattern — used in search filters everywhere
- ✓Always clean text data before grouping or joining on text columns
🏢 Real-World Application
String functions are used constantly in data cleaning and reporting. Customer names stored inconsistently ("rahul sharma", "RAHUL SHARMA", " Rahul Sharma ") are normalised using LOWER() and TRIM(). Phone numbers stored with and without country codes are standardised using REPLACE(phone, '+91', ''). Extracting the domain from email addresses uses SUBSTRING(email, CHARINDEX('@', email) + 1). Product codes that follow a pattern (first 3 characters = category) use LEFT(product_code, 3). Any time you work with text columns in real databases, string functions are your tools for cleaning and transforming them.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What are string functions in SQL?
String functions manipulate text data. Common ones include UPPER(), LOWER(), LENGTH(), CONCAT(), SUBSTRING(), TRIM(), REPLACE(), LEFT(), RIGHT(), CHARINDEX(), and LIKE for pattern matching.
How do I concatenate strings in SQL?
Use CONCAT(str1, str2) or the || operator (in PostgreSQL/SQLite): CONCAT(first_name, ' ', last_name). In SQL Server you can also use +: first_name + ' ' + last_name.
How do I extract part of a string in SQL?
Use SUBSTRING(string, start, length). SUBSTRING('New Delhi', 1, 3) returns 'New'. In MySQL you can also use LEFT(string, n) and RIGHT(string, n) for the leftmost or rightmost n characters.
How do I remove spaces from a string in SQL?
TRIM() removes leading and trailing spaces. LTRIM() removes only leading spaces. RTRIM() removes only trailing spaces. To remove all spaces including in the middle, use REPLACE(col, ' ', '').
How does LIKE work in SQL?
LIKE is used for pattern matching in WHERE clauses. % matches zero or more characters; _ matches exactly one character. WHERE email LIKE '%@gmail.com' finds all Gmail users. WHERE code LIKE 'A__' finds codes starting with A followed by exactly two characters.
✏️ Practice Exercise
Write a query to find all customers whose city starts with "N" and display their name in uppercase.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.