Text columns such as names, emails and codes often need cleaning or reshaping before they are useful: trimming spaces, changing case, extracting part of a value or joining several columns into one label. SQL’s string functions do this inside the query, so the database returns data that is already formatted. This lesson covers the most used ones and the LIKE operator for pattern matching.
In this lesson you will learn
UPPER,LOWERandLENGTH- Extracting text with
SUBSTR - Joining strings with
||andCONCAT TRIM,REPLACEandINSTR- Pattern matching with
LIKE,%and_
Case, Length, Substrings and Concatenation
SELECT name,
UPPER(name) AS upper_name,
LOWER(city) AS lower_city,
LENGTH(name) AS letters,
SUBSTR(name, 1, 3) AS first_three
FROM students
WHERE dept = 'BBA';
-- Concatenation with || (standard SQL). MySQL uses CONCAT()
SELECT name || ' (' || dept || ', ' || city || ')' AS label
FROM students
WHERE id <= 3;
Output
+--------+------------+------------+---------+-------------+
| name | upper_name | lower_city | letters | first_three |
+--------+------------+------------+---------+-------------+
| Imran | IMRAN | khulna | 5 | Imr |
| Sabbir | SABBIR | rajshahi | 6 | Sab |
+--------+------------+------------+---------+-------------+
(2 rows)
+------------------------+
| label |
+------------------------+
| Nusrat (CSE, Dhaka) |
| Arif (EEE, Chattogram) |
| Tania (CSE, Dhaka) |
+------------------------+
(3 rows)
UPPER()andLOWER()change letter case. ComparingLOWER(email) = LOWER(?)makes a search case-insensitive.LENGTH()counts characters. (SQL Server calls itLEN().)SUBSTR(text, start, length)extracts part of a string. Positions start at 1, not 0. MySQL and PostgreSQL also acceptSUBSTRING().||joins strings in standard SQL, SQLite, PostgreSQL and Oracle. MySQL usesCONCAT(a, b, ...), and SQL Server uses+orCONCAT. If any part is NULL,||gives NULL, so wrap optional parts inCOALESCE.
Cleaning Text and Pattern Matching
SELECT TRIM(' hello ') AS trimmed,
REPLACE('2026-11-15', '-', '/') AS replaced,
INSTR('database', 'base') AS position_of_base;
-- Pattern matching with LIKE: % = any text, _ = one character
SELECT name FROM students WHERE name LIKE 'N%';
SELECT name FROM students WHERE name LIKE '_a%';
Output
+---------+------------+------------------+
| trimmed | replaced | position_of_base |
+---------+------------+------------------+
| hello | 2026/11/15 | 5 |
+---------+------------+------------------+
(1 row)
+--------+
| name |
+--------+
| Nusrat |
| Nadia |
+--------+
(2 rows)
+--------+
| name |
+--------+
| Tania |
| Rafi |
| Sabbir |
| Nadia |
+--------+
(4 rows)
TRIM()removes spaces from both ends;LTRIM()andRTRIM()remove them from one side.REPLACE(text, old, new)swaps every occurrence of a substring.INSTR(text, part)returns the position wherepartstarts, or 0 if it is missing. (PostgreSQL usesPOSITION(part IN text).)LIKE 'N%'matches names starting with N.%means any number of characters, including none.LIKE '_a%'matches names whose second letter is “a”._means exactly one character.- In SQLite and MySQL,
LIKEignores case for English letters by default; in PostgreSQL it is case-sensitive, andILIKEignores case.
String Function Cheat Sheet
| Function | Example | Result |
|---|---|---|
UPPER(s) | UPPER('sql') | SQL |
LENGTH(s) | LENGTH('Dhaka') | 5 |
SUBSTR(s, start, n) | SUBSTR('database', 5, 4) | base |
TRIM(s) | TRIM(' hi ') | hi |
REPLACE(s, a, b) | REPLACE('a-b', '-', '+') | a+b |
INSTR(s, t) | INSTR('hello', 'l') | 3 |
a || b | 'Core' || 'Tech' | CoreTech |
Common Mistakes
- Counting positions from 0; SQL strings start at 1.
- Concatenating with a NULL and getting NULL back.
- Starting a pattern with
%on large tables:LIKE '%abc'cannot use a normal index and scans every row. - Applying functions to a column in
WHERE, such asWHERE UPPER(name) = 'ARIF', which also stops a plain index from being used.
Practice Exercises
Exercise 1. Show each student’s initials, such as “N.” for Nusrat, with their department.
Show answer
SELECT SUBSTR(name, 1, 1) || '.' AS initial, dept
FROM students;
Exercise 2. Find courses whose title contains the word “Data”.
Show answer
SELECT title FROM courses WHERE title LIKE '%Data%';
It returns “Data Structures” and “Databases”.
Frequently Asked Questions
How do I search for a literal % or _ with LIKE?
Use an escape character: WHERE code LIKE '50!%' ESCAPE '!' matches the text “50%” exactly.
Should I format text in SQL or in my application?
Cleaning and filtering belong in SQL, close to the data. Display formatting, such as currency symbols or translations, is often easier in the application.
Summary
UPPER,LOWER,LENGTH,SUBSTR,TRIMandREPLACEreshape text.- Join strings with
||(orCONCATin MySQL); watch out for NULLs. LIKEmatches patterns:%for any text,_for one character.- Function names vary slightly between databases.
Next: SQL Date and Time Functions.
