By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(CONCAT, SUBSTRING, REPLACE, UPPER/LOWER, TRIM)
You’re analyzing customer data, and your boss asks: "Can you merge first and last names into a full name column, clean up extra spaces, and extract the first 3 characters of each email for a quick lookup?"
If you don’t know string functions, you’ll waste hours manually editing data in Excel—or worse, writing brittle Python scripts. String functions are your Swiss Army knife for cleaning, transforming, and extracting text in SQL. They let you: - Fix messy data (e.g., " [email protected] " → "[email protected]").- Combine fields (e.g., first_name + last_name → "John Doe").- Extract substrings (e.g., grab the first 3 letters of a product code).- Standardize text (e.g., "USA" vs "usa" → all uppercase).
" [email protected] "
"[email protected]"
first_name + last_name
"John Doe"
"USA"
"usa"
Without them, you’re stuck with:❌ Manual data cleaning (slow, error-prone).❌ Broken joins (e.g., "New York" vs "new york" won’t match).❌ Ugly reports (e.g., " Customer Name: John " instead of "Customer Name: John").
"New York"
"new york"
" Customer Name: John "
"Customer Name: John"
Real-world scenario:You inherit a database where phone numbers are stored as "(123) 456-7890" but your CRM expects "1234567890". With REPLACE and TRIM, you fix this in one SQL query instead of exporting to Excel.
"(123) 456-7890"
"1234567890"
REPLACE
TRIM
CONCAT()
||
"SELECT * FROM " + table_name
SUBSTRING()
SUBSTR()
SUBSTRING(str, pos, len)
SUBSTRING(str, start, length)
REPLACE()
"Street"
"St"
REPLACE(phone, "-", "")
WHERE
UPPER()
LOWER()
WHERE LOWER(country) = 'usa'
"ß"
"SS"
TRIM()
LTRIM()
RTRIM()
"John"
" John "
LEN()
LENGTH()
WHERE LENGTH(password) >= 8
LEFT()
RIGHT()
LEFT(email, 3)
CHARINDEX()
POSITION()
INSTR()
CHARINDEX('@', email)
Clean a customers table with: - Extra spaces in names.- Inconsistent email formats (e.g., "[email protected]").- Phone numbers with parentheses/dashes.
customers
CREATE TABLE customers ( id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), email VARCHAR(100), phone VARCHAR(20) ); INSERT INTO customers VALUES (1, ' John ', ' Doe ', '[email protected]', '(123) 456-7890'), (2, 'Jane', 'Smith', '[email protected]', '555-123-4567'), (3, ' Bob ', 'Johnson', '[email protected]', '123.456.7890');
-- 1. Trim spaces from names UPDATE customers SET first_name = TRIM(first_name), last_name = TRIM(last_name); -- 2. Standardize email case (lowercase) UPDATE customers SET email = LOWER(email); -- 3. Remove dots from emails (e.g., "[email protected]" → "[email protected]") UPDATE customers SET email = REPLACE(email, '.', ''); -- 4. Clean phone numbers (remove non-digits) UPDATE customers SET phone = REPLACE(REPLACE(REPLACE(phone, '(', ''), ')', ''), '-', ''); -- 5. Create a full_name column ALTER TABLE customers ADD COLUMN full_name VARCHAR(100); UPDATE customers SET full_name = CONCAT(TRIM(first_name), ' ', TRIM(last_name)); -- 6. Extract the first 3 letters of the email (for quick lookup) ALTER TABLE customers ADD COLUMN email_prefix VARCHAR(3); UPDATE customers SET email_prefix = LEFT(email, 3);
SELECT * FROM customers;
Expected Output:| id | first_name | last_name | email | phone | full_name | email_prefix | |----|------------|-----------|---------------------|------------|-------------|--------------| | 1 | John | Doe | [email protected] | 1234567890 | John Doe | joh | | 2 | Jane | Smith | [email protected] | 5551234567 | Jane Smith | jan | | 3 | Bob | Johnson | [email protected] | 1234567890 | Bob Johnson | bob |
-- ✅ SAFE (parameterized) EXEC sp_executesql N'SELECT * FROM users WHERE name = @name', N'@name VARCHAR(50)', @name = @user_input; ```
-- ✅ Fast (uses index) SELECT * FROM users WHERE email = '[email protected]'; `` - Pre-clean data: If possible, clean data at insertion time (e.g.,TRIM()inINSERT` statements).
`` - Pre-clean data: If possible, clean data at insertion time (e.g.,
in
UPPER
LOWER
sql -- Standardize phone numbers: remove all non-digits UPDATE customers SET phone = REGEXP_REPLACE(phone, '[^0-9]', '');
-- Ensure phone numbers are 10 digits SELECT COUNT(*) FROM customers WHERE LENGTH(phone) != 10; ```
WHERE country = 'USA'
'usa'
SUBSTRING(email, 1, 3)
SELECT LENGTH(email) FROM users LIMIT 10
REGEXP_REPLACE()
UPPER('ß')
'SS'
COLLATE
UPPER(name COLLATE utf8_general_ci)
[email protected]
example.com
Answer: SUBSTRING(email, CHARINDEX('@', email) + 1, LENGTH(email)) (SQL Server) or SUBSTRING(email, POSITION('@' IN email) + 1) (PostgreSQL).
SUBSTRING(email, CHARINDEX('@', email) + 1, LENGTH(email))
SUBSTRING(email, POSITION('@' IN email) + 1)
Case standardization: "Ensure all country codes are uppercase in a WHERE clause."
Answer: WHERE UPPER(country) = 'USA'.
WHERE UPPER(country) = 'USA'
Data cleaning: "Remove all non-alphanumeric characters from a phone number."
REGEXP_REPLACE(phone, '[^0-9]', '')
+
CONCAT('a', NULL)
'a'
'a' + NULL
NULL
SUBSTRING
SUBSTRING(str, 1, 3)
str[0:3]
TRIM('x' FROM str)
'x'
"You need to merge first_name and last_name into a full_name column, but some names have extra spaces. Which function do you use?" - Answer: CONCAT(TRIM(first_name), ' ', TRIM(last_name)).- Why? TRIM() removes extra spaces; CONCAT() merges the strings.
first_name
last_name
full_name
CONCAT(TRIM(first_name), ' ', TRIM(last_name))
Challenge:Given a table products with a sku column like "ABC-123-XYZ", extract the middle part ("123") into a new column product_id.
products
sku
"ABC-123-XYZ"
"123"
product_id
Solution:
-- PostgreSQL/MySQL UPDATE products SET product_id = SUBSTRING(sku, 5, 3); -- SQL Server UPDATE products SET product_id = SUBSTRING(sku, 5, 3);
Why it works:- SUBSTRING(sku, 5, 3) starts at position 5 ("-" is position 4) and takes 3 characters.
SUBSTRING(sku, 5, 3)
"-"
CONCAT(str1, str2, ...)
CONCAT('A', 'B')
'AB'
str1 || str2
'A' || 'B'
SUBSTRING('abc', 2, 1)
'b'
LEFT(str, length)
LEFT('abc', 2)
'ab'
RIGHT(str, length)
RIGHT('abc', 2)
'bc'
REPLACE(str, old, new)
REPLACE('abc', 'a', 'x')
'xbc'
UPPER(str)
UPPER('aBc')
'ABC'
LOWER(str)
LOWER('aBc')
'abc'
TRIM(str)
TRIM(' a ')
LTRIM(str)
LTRIM(' a')
RTRIM(str)
RTRIM('a ')
LENGTH(str)
LENGTH('abc')
3
CHARINDEX(substr, str)
CHARINDEX('b', 'abc')
2
POSITION(substr IN str)
POSITION('b' IN 'abc')
⚠️ Exam Traps:- SUBSTRING('abc', 0, 1) → Error in most databases (1-based indexing).- CONCAT('a', NULL) → 'a' (ignores NULL), but 'a' + NULL → NULL (SQL Server).- TRIM('x' FROM 'xxabcxx') → 'abc' (removes 'x' from both sides).
SUBSTRING('abc', 0, 1)
TRIM('x' FROM 'xxabcxx')
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.