SQL String Functions
String functions clean and reshape text: change case, cut out pieces, join values together. Names vary a little between databases; the examples here use SQLite, with notes for others.
Updated December 1, 2026
Syntax
UPPER(text) LOWER(text) LENGTH(text)
SUBSTR(text, start, length) REPLACE(text, old, new)
text1 || text2
Change case and measure length
UPPER, LOWER and LENGTH work the same way in almost every database.
SELECT name, UPPER(name) AS shout, LENGTH(name) AS letters
FROM customers;
| name | shout | letters |
|---|---|---|
| Amara Obi | AMARA OBI | 9 |
| Bruno Costa | BRUNO COSTA | 11 |
| Chen Wei | CHEN WEI | 8 |
| Dana Levi | DANA LEVI | 9 |
Join text together
|| concatenates. MySQL and SQL Server use CONCAT(a, b) instead.
SELECT name || ' (' || country || ')' AS label
FROM customers;
| label |
|---|
| Amara Obi (Nigeria) |
| Bruno Costa (Brazil) |
| Chen Wei (China) |
| Dana Levi (Israel) |
Cut and replace
SUBSTR(text, start) counts from 1 and, without a length, runs to the end. INSTR finds where a piece of text starts, so together they can take everything after the first space.
SELECT name,
SUBSTR(name, INSTR(name, ' ') + 1) AS last_name,
REPLACE(LOWER(title), ' ', '-') AS title_slug
FROM employees;
| name | last_name | title_slug |
|---|---|---|
| Maya Chen | Chen | vp-engineering |
| Omar Haddad | Haddad | staff-engineer |
| Lena Park | Park | senior-engineer |
| Ravi Patel | Patel | engineer |
| Sofia Rossi | Rossi | engineer |
Common mistakes
- Counting from 0. SQL string positions start at 1.
- Concatenating NULL:
'a' || NULLis NULL. Wrap values inCOALESCEfirst. - Comparing text with stray spaces. Use
TRIMwhen data comes from forms or files.
Practice String Functions interactively
Write real queries in your browser and get instant feedback in the lesson “Text functions” (Unit 7). New learners can start free from the first lesson.
Frequently asked questions
How do I concatenate strings in SQL?
Use || in SQLite, PostgreSQL and Oracle; use CONCAT() in MySQL and SQL Server (which also supports +).
What is the difference between LENGTH and LEN?
Same idea, different names: LEN in SQL Server, LENGTH elsewhere.