☰ Learn SQL Tutorial Menu
String Functions
Written by Faisal Adnan · Updated 16 Sept 2026 · 3 min read
Real-world text data is messy — inconsistent casing, extra whitespace, names that need combining. String functions clean and transform text directly in SQL.
Concatenation
-- ANSI standard (works in PostgreSQL, SQLite, SQL Server 2012+)
SELECT first_name || ' ' || last_name AS full_name FROM employees;
-- MySQL (also works in SQL Server)
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
Changing case
SELECT UPPER(first_name), LOWER(last_name) FROM employees;
Trimming whitespace
SELECT TRIM(' padded value ') AS cleaned;
SELECT LTRIM(' left padded');
SELECT RTRIM('right padded ');
Length
SELECT product_name, LENGTH(product_name) AS name_length FROM products;
SQL Server uses LEN() instead of LENGTH().
Substrings
-- First 3 characters
SELECT SUBSTRING(product_name, 1, 3) FROM products;
SQLite uses SUBSTR(); the rest generally accept both SUBSTR and SUBSTRING.
Replacing text
SELECT REPLACE(email, '@csa-demo.local', '@company.com') FROM employees;
Finding position
-- Position of '@' within the email (1-based)
SELECT email, INSTR(email, '@') FROM employees; -- SQLite / MySQL
SELECT email, POSITION('@' IN email) FROM employees; -- PostgreSQL
SELECT email, CHARINDEX('@', email) FROM employees; -- SQL Server
Putting it together
SELECT
UPPER(TRIM(first_name)) || ' ' || UPPER(TRIM(last_name)) AS display_name,
LENGTH(first_name) AS first_name_length
FROM employees
WHERE first_name LIKE 'A%';
Open the Code Playground, pick any SQL engine (SQLite, PostgreSQL, MySQL, or SQL Server), and paste these queries in directly — the practice database above is already loaded for you, every time.
Lesson 15 of 27
Sign in to track your progress and earn XP for every lesson you finish.
Practice tip: the built-in code runner is being upgraded. Until it is back, copy the examples into any SQL or Python tool you have (for example SQLite, PostgreSQL, MySQL, SQL Server or a free online editor) and run them there. You can keep reading and earning XP here.
