Reading history

SQL String Functions: UPPER, SUBSTR, TRIM, REPLACE and LIKE

4 min read 0 views

Learn SQL string functions with examples: UPPER, LOWER, LENGTH, SUBSTR, concatenation, TRIM, REPLACE, INSTR and pattern matching with LIKE.

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, LOWER and LENGTH
  • Extracting text with SUBSTR
  • Joining strings with || and CONCAT
  • TRIM, REPLACE and INSTR
  • 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() and LOWER() change letter case. Comparing LOWER(email) = LOWER(?) makes a search case-insensitive.
  • LENGTH() counts characters. (SQL Server calls it LEN().)
  • SUBSTR(text, start, length) extracts part of a string. Positions start at 1, not 0. MySQL and PostgreSQL also accept SUBSTRING().
  • || joins strings in standard SQL, SQLite, PostgreSQL and Oracle. MySQL uses CONCAT(a, b, ...), and SQL Server uses + or CONCAT. If any part is NULL, || gives NULL, so wrap optional parts in COALESCE.

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() and RTRIM() remove them from one side.
  • REPLACE(text, old, new) swaps every occurrence of a substring.
  • INSTR(text, part) returns the position where part starts, or 0 if it is missing. (PostgreSQL uses POSITION(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, LIKE ignores case for English letters by default; in PostgreSQL it is case-sensitive, and ILIKE ignores case.

String Function Cheat Sheet

FunctionExampleResult
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

  1. Counting positions from 0; SQL strings start at 1.
  2. Concatenating with a NULL and getting NULL back.
  3. Starting a pattern with % on large tables: LIKE '%abc' cannot use a normal index and scans every row.
  4. Applying functions to a column in WHERE, such as WHERE 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, TRIM and REPLACE reshape text.
  • Join strings with || (or CONCAT in MySQL); watch out for NULLs.
  • LIKE matches patterns: % for any text, _ for one character.
  • Function names vary slightly between databases.

Next: SQL Date and Time Functions.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *