01 / 22

SQL String
Functions

Learn how to manipulate text data in SQL using SUBSTRING, CONCAT, and TRIM: the essential toolkit for working with strings in any database.

SQL

What Are Strings in SQL?

A string is any sequence of characters stored as text in a database column. Names, emails, addresses, product descriptions: they are all strings. In SQL, string values are enclosed in single quotes.

SELECT 'Hello, World!' AS greeting;

Why String Functions Matter

Data Cleaning
Remove unwanted spaces, fix inconsistent formatting, and standardize entries before analysis.
Data Extraction
Pull out area codes from phone numbers, domains from emails, or initials from names.
Data Presentation
Combine first and last names into full names, build formatted labels, or create readable reports.

The Big Three: SUBSTRING, CONCAT, TRIM

SUBSTRING
Extracts a portion of a string starting at a given position for a given length.
CONCAT
Joins two or more strings together into a single combined string.
TRIM
Removes leading and/or trailing whitespace (or specified characters) from a string.

SUBSTRING: Extract a Piece

SUBSTRING(string, start, length)
1
string: the source text (column name or literal value).
2
start: the position where extraction begins. In most SQL dialects, positions start at 1 (not 0).
3
length: how many characters to extract. If omitted in some dialects, it takes everything from start to the end.

Visualizing SUBSTRING

SUBSTRING('DATABASE', 5, 4)
Source string with character positions:
D1
A2
T3
A4
B5
A6
S7
E8
Result: 'BASE'

CONCAT: Join Strings Together

CONCAT(string1, string2, ...)

CONCAT takes two or more string arguments and returns them glued together in order. It does not add any separator automatically: if you want a space between words, you must include it yourself.

SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM students;
Result: 'Ada Lovelace'

Visualizing CONCAT

CONCAT('SQL', ' is ', 'fun')
'SQL' + ' is ' + 'fun' 'SQL is fun'

TRIM: Clean Up Whitespace

TRIM(string) LTRIM(string) -- left trim only RTRIM(string) -- right trim only

TRIM removes spaces (or specified characters) from both ends of a string. LTRIM strips the left side only; RTRIM strips the right side only. Inner spaces are never removed.

SELECT TRIM(' hello ') AS cleaned;
Result: 'hello'

Visualizing TRIM

TRIM(' OK ')
Before TRIM:
 1
 2
O3
K4
 5
 6
After TRIM:
O1
K2

SUBSTRING vs CONCAT vs TRIM

SUBSTRING CONCAT TRIM
Purpose Extract part Join parts Remove edges
Inputs 1 string + position 2+ strings 1 string
Output length Shorter or equal Longer or equal Shorter or equal
Changes content? Selects a portion Combines all Removes spaces

How to Apply String Functions

1
Identify the goal. Do you need to extract, combine, or clean the text?
2
Choose the right function. SUBSTRING for extracting, CONCAT for joining, TRIM for cleaning whitespace.
3
Determine the parameters. For SUBSTRING: what position and length? For CONCAT: what pieces and separators?
4
Nest if needed. You can combine functions: CONCAT(TRIM(first), ' ', TRIM(last)).
5
Test with sample data. Run your query on a few rows to verify the output before applying to the whole table.

Example 1: Extract Area Codes

Given a phone column with values like '(415) 555-1234', extract just the 3-digit area code.

SELECT SUBSTRING(phone, 2, 3) AS area_code FROM contacts;
How it works on '(415) 555-1234':
(1
42
13
54
)5
 6
57
58
59
Result: '415'

Example 2: Build a Full Name

Combine first_name and last_name columns into a formatted display name, cleaning any extra spaces first.

SELECT CONCAT( TRIM(first_name), ' ', TRIM(last_name) ) AS full_name FROM employees;
Input: ' Ada ' + ' Lovelace ' → Output: 'Ada Lovelace'

Example 3: Create a Username

Generate a username from first initial + last name: 'Ada' + 'Lovelace' → 'ALovelace'.

SELECT CONCAT( SUBSTRING(first_name, 1, 1), last_name ) AS username FROM employees;
SUBSTRING('Ada', 1, 1) → 'A' last_name → 'Lovelace' CONCAT → 'ALovelace'

Common Mistakes

✕ Off-by-One Position
SQL string positions start at 1, not 0. Using 0 as the start returns unexpected results or errors depending on the dialect.
✕ Forgetting the Separator
CONCAT does not insert spaces. CONCAT('A','B') gives 'AB', not 'A B'. Always include an explicit space string.
✕ TRIM Only Removes Edges
TRIM does not remove spaces in the middle of a string. TRIM('a b') still returns 'a b'.

Quick Reference Cheat Sheet

Function Syntax Example Result
SUBSTRING SUBSTRING(str, start, len) SUBSTRING('Hello', 2, 3) 'ell'
CONCAT CONCAT(str1, str2, ...) CONCAT('Hi', ' ', 'there') 'Hi there'
TRIM TRIM(str) TRIM(' Hi ') 'Hi'
LTRIM LTRIM(str) LTRIM(' Hi ') 'Hi '
RTRIM RTRIM(str) RTRIM(' Hi ') ' Hi'

Question 1 of 5

What does SUBSTRING('COMPUTER', 4, 3) return?

Question 2 of 5

Which SQL expression correctly joins two separate strings 'Hello' and 'World' with a space between them?

Question 3 of 5

What does TRIM(' data ') return?

Question 4 of 5

A table has columns first_name and last_name. Which query produces usernames like 'ALovelace' (first initial + last name)?

Question 5 of 5

In standard SQL, what is the starting index of the first character in a string?