SQL String Functions
SQL has many functions for working with text: changing case, cutting out parts, searching, replacing and formatting. Names vary between databases; this lesson uses SQLite's and lists the others.
Overview
| Task | SQLite | MySQL | PostgreSQL | SQL Server |
|---|---|---|---|---|
| Length | length(s) |
CHAR_LENGTH(s) |
length(s) |
LEN(s) |
| Upper / lower | upper(s), lower(s) |
same | same | same |
| Part of a string | substr(s, start, len) |
SUBSTRING(s, start, len) |
substr / substring |
SUBSTRING(s, start, len) |
| Find position | instr(s, sub) |
LOCATE(sub, s) |
strpos(s, sub) |
CHARINDEX(sub, s) |
| Replace | replace(s, from, to) |
same | same | same |
| Trim spaces | trim, ltrim, rtrim |
same | same | same (TRIM in 2017+) |
| Concatenate | `a | b, concat(a, b)` |
CONCAT(a, b) |
|
| Format | printf / format |
FORMAT, LPAD |
format, to_char, lpad |
FORMAT |
Positions in SQL strings start at 1, not 0.
Case and length
SELECT name,
upper(name) AS upper_name,
lower(name) AS lower_name,
length(name) AS len
FROM suppliers
WHERE supplier_id <= 3
ORDER BY supplier_id;name | upper_name | lower_name | len
--------------------+---------------------+---------------------+----
Highland Roasters | HIGHLAND ROASTERS | highland roasters | 17
Golden Crust Bakery | GOLDEN CRUST BAKERY | golden crust bakery | 19
Alpine Dairy Co. | ALPINE DAIRY CO. | alpine dairy co. | 16
(3 rows)
Cutting strings apart
substr(s, start, length) takes a piece; instr(s, sub) finds where a piece starts (0 if not found). Together they split an e-mail address into user and domain:
SELECT email,
substr(email, 1, instr(email, '@') - 1) AS user,
substr(email, instr(email, '@') + 1) AS domain
FROM customers
WHERE customer_id IN (1, 2, 15)
ORDER BY customer_id;email | user | domain
--------------------------+---------------+------------
ava.nguyen@example.com | ava.nguyen | example.com
liam.schmidt@example.com | liam.schmidt | example.com
lena.andersen@example.com | lena.andersen | example.com
(3 rows)
A negative start counts from the end: substr('2025-06-13', -2) is '13'.
Replacing and trimming
SELECT replace(unit, ' x ', '×') AS replaced,
trim(' padded ') AS trimmed,
'[' || rtrim('xx--', '-') || ']' AS rtrimmed,
replace(lower('Noise-Cancelling Headphones'), ' ', '-') AS slug
FROM products
WHERE product_id = 4;replaced | trimmed | rtrimmed | slug
---------+---------+----------+----------------------------
6×500 ml | padded | [xx] | noise-cancelling-headphones
(1 row)
Building text
printf (also called format in SQLite 3.38+) formats numbers and pads text, like in C:
SELECT printf('%-28s %8.2f', name, price) AS price_line,
printf('#%05d', product_id) AS code
FROM products
WHERE category_id = 6
ORDER BY price DESC;price_line | code
--------------------------------------+-------
Noise-Cancelling Headphones 149.00 | #00023
Mechanical Keyboard 89.00 | #00022
Power Bank 29.50 | #00024
Wireless Mouse 24.99 | #00021
USB-C Cable 9.99 | #00020
(5 rows)
| Format | Meaning |
|---|---|
%s |
text; %-10s left-aligned in 10 characters |
%d |
integer; %05d zero-padded to 5 digits |
%.2f |
number with 2 decimals; %8.2f right-aligned in 8 characters |
Initials and other combinations
Functions nest freely. Employee initials and a login name:
SELECT first_name, last_name,
substr(first_name, 1, 1) || substr(last_name, 1, 1) AS initials,
lower(substr(first_name, 1, 1) || last_name) AS login
FROM employees
WHERE employee_id IN (1, 4, 10)
ORDER BY employee_id;first_name | last_name | initials | login
-----------+-----------+----------+---------
Grace | Holt | GH | gholt
Rosa | Delgado | RD | rdelgado
Elena | Popescu | EP | epopescu
(3 rows)
Other handy functions
| Function | Example | Result |
|---|---|---|
unicode(s) / char(n) |
unicode('A'), char(65) |
65, 'A' |
hex(s) |
hex('Hi') |
'4869' |
quote(s) |
quote('It''s') |
'It''s' (as an SQL literal) |
group_concat(col, sep) |
per group | 'a, b, c' |
s LIKE p, s GLOB p |
pattern match | 1 / 0 |
Tip: Functions on a column in
WHERE(WHERE lower(email) = …) prevent the use of a normal index on that column. For frequent case-insensitive lookups, store a normalized value or create an index on the expression:CREATE INDEX idx ON customers (lower(email)).
Test yourself
What does substr('TAD Market', 5, 3) return?
- 'Mar'
- 'Ma'
- ' Ma'
- 'ark'
What does instr('hello', 'z') return?
- -1
- 0
- NULL
- An error