Text columns are messy: names in mixed case, extra spaces, emails you need to split, product codes that hide a category. SQL string functions let you clean and reshape text directly in the query, before the data ever reaches Excel or Power BI.
This guide covers the string functions analysts use most, with the differences between MySQL, PostgreSQL and SQL Server.
Quick reference
| Task | MySQL | PostgreSQL | SQL Server |
|---|---|---|---|
| Join text | CONCAT(a, ' ', b) | CONCAT(a, ' ', b) or a || ' ' || b | CONCAT(a, ' ', b) |
| Length | CHAR_LENGTH(s) | LENGTH(s) | LEN(s) |
| Upper / lower | UPPER(s), LOWER(s) in all three | ||
| Remove spaces | TRIM(s), LTRIM(s), RTRIM(s) (TRIM needs SQL Server 2017+) | ||
| Part of text | SUBSTRING(s, start, length), LEFT(s, n), RIGHT(s, n) | ||
| Find position | LOCATE('@', s) | POSITION('@' IN s) | CHARINDEX('@', s) |
| Replace | REPLACE(s, 'old', 'new') | ||
| Combine many rows | GROUP_CONCAT(s SEPARATOR ', ') | STRING_AGG(s, ', ') | STRING_AGG(s, ', ') |
In MySQL, LENGTH() counts bytes, so text with Hindi or other non-English characters gives a bigger number. Use CHAR_LENGTH() to count characters.
The examples use a customers table with first_name, last_name, email, phone and city.
1. Full name
SELECT CONCAT(first_name, ' ', last_name) AS full_name
FROM customers;
If either name can be NULL, read the NULL note at the end of this post, because CONCAT behaves differently in MySQL.
2. Clean names: trim and fix case
SELECT UPPER(LEFT(TRIM(first_name), 1)) || LOWER(SUBSTRING(TRIM(first_name), 2, 100)) AS first_name_clean
FROM customers; -- PostgreSQL
This makes " rAHUL" into "Rahul". PostgreSQL also has INITCAP(first_name), which capitalises every word in one step. In MySQL and SQL Server, use CONCAT(...) instead of ||.
3. Email domain
-- PostgreSQL
SELECT SPLIT_PART(email, '@', 2) AS domain FROM customers;
-- MySQL
SELECT SUBSTRING_INDEX(email, '@', -1) AS domain FROM customers;
-- SQL Server
SELECT SUBSTRING(email, CHARINDEX('@', email) + 1, LEN(email)) AS domain FROM customers;
Then count customers per domain with GROUP BY to see how many use Gmail versus company addresses.
4. Last 10 digits of a phone number
SELECT RIGHT(REPLACE(REPLACE(phone, ' ', ''), '-', ''), 10) AS mobile
FROM customers;
Removes spaces and hyphens, then keeps the last 10 digits, so "+91 98765-43210" becomes "9876543210".
5. Search with LIKE
SELECT * FROM customers WHERE email LIKE '%@gmail.com';
SELECT * FROM customers WHERE city LIKE 'Ma%'; -- starts with Ma
SELECT * FROM customers WHERE last_name LIKE '_a%'; -- second letter is a
% matches any number of characters and _ exactly one. Whether LIKE is case-sensitive depends on the database: PostgreSQL is case-sensitive (use ILIKE to ignore case), while MySQL and SQL Server usually are not, depending on the collation. To be safe everywhere: WHERE LOWER(city) LIKE 'ma%'.
6. Standardise values
SELECT CASE
WHEN UPPER(TRIM(city)) IN ('BOMBAY', 'MUMBAI') THEN 'Mumbai'
WHEN UPPER(TRIM(city)) IN ('BANGALORE', 'BENGALURU') THEN 'Bengaluru'
ELSE TRIM(city)
END AS city_clean
FROM customers;
More on CASE: SQL CASE WHEN.
7. List values in one cell
-- PostgreSQL / SQL Server
SELECT city, STRING_AGG(first_name, ', ') AS customers
FROM customers
GROUP BY city;
-- MySQL
SELECT city, GROUP_CONCAT(first_name SEPARATOR ', ') AS customers
FROM customers
GROUP BY city;
Useful for small summaries, such as all products in an order on one line.
8. Find rows with hidden spaces
SELECT *
FROM customers
WHERE email <> TRIM(email);
Leading or trailing spaces are a common reason joins and lookups fail. Fix them with UPDATE customers SET email = TRIM(email); after checking the result.
NULL and string functions
CONCATin PostgreSQL and SQL Server treats NULL as empty text, so a missing last name still gives the first name.- In MySQL,
CONCATreturns NULL if any argument is NULL. UseCONCAT_WS(' ', first_name, last_name), which skips NULLs, or wrap columns inCOALESCE(col, ''). - The
||operator in PostgreSQL returns NULL if any part is NULL.
Details: SQL NULL handling.
Practice task
From a customers table, return one row per email domain with the number of customers and a comma-separated list of their cleaned first names, sorted by the number of customers.
Doing the same in Excel? See Excel text functions.
