String Functions
Text data is rarely in exactly the shape you need. String functions change case, measure length, extract parts, trim spaces, find and replace — essential for cleaning data, building labels and searching. Function names vary more between databases here than anywhere else, so the table at the end maps the common ones.
Changing case
SQL
SELECT name, upper(name) AS shouting, lower(name) AS quiet
FROM products
WHERE id IN (1, 4);
Result 2 rows
| name | shouting | quiet |
| The Patient Gardener | THE PATIENT GARDENER | the patient gardener |
| Wireless Earbuds | WIRELESS EARBUDS | wireless earbuds |
A common use is case-insensitive comparison: WHERE lower(email) = lower('Ana.Pereira@Example.com'). (SQLite's built-in upper/lower only convert ASCII letters; other databases handle accented letters too.)
Length
SQL
SELECT name, length(name) AS characters
FROM products
ORDER BY characters DESC
LIMIT 3;
Result 3 rows
| name | characters |
| Learning SQL Step by Step | 25 |
| The Patient Gardener | 20 |
| Mechanical Keyboard | 19 |
length counts characters in SQLite, PostgreSQL and Oracle. In MySQL, LENGTH counts bytes and CHAR_LENGTH counts characters; SQL Server uses LEN.
Extracting part of a string
SQL
SELECT email,
substr(email, 1, instr(email, '@') - 1) AS username,
substr(email, instr(email, '@') + 1) AS domain
FROM customers
WHERE id <= 3;
Result 3 rows
| email | username | domain |
| ana.pereira@example.com | ana.pereira | example.com |
| ben.carter@example.com | ben.carter | example.com |
| chloe.martin@example.com | chloe.martin | example.com |
substr(text, start, length) extracts characters; positions start at 1, not 0. Leave out the length to take everything to the end. instr(text, find) returns the position of the first match, or 0. (Standard SQL spells these SUBSTRING and POSITION(find IN text).)
SQL
SELECT first_name, substr(first_name, 1, 1) || '. ' || last_name AS short_name
FROM employees
WHERE department_id = 4;
Result 2 rows
| first_name | short_name |
| Ethan | E. Walsh |
| Priya | P. Nair |
Trimming and padding
SQL
SELECT '[' || trim(' spaced out ') || ']' AS trimmed,
'[' || ltrim(' left') || ']' AS left_trimmed,
trim('--note--', '-') AS custom;
Result 1 row
| trimmed | left_trimmed | custom |
| [spaced out] | [left] | note |
trim removes spaces (or the characters you list) from both ends — useful when cleaning imported data. For padding, MySQL, PostgreSQL and Oracle have LPAD and RPAD; in SQLite, printf / format does the job:
SQL
SELECT format('%05d', id) AS order_ref, format('[%-10s]', status) AS padded
FROM orders
WHERE id IN (3, 12);
Result 2 rows
| order_ref | padded |
| 00003 | [delivered ] |
| 00012 | [pending ] |
Replacing text
SQL
SELECT name, replace(name, ' ', '-') AS slug
FROM products
WHERE category = 'Home';
Result 3 rows
| name | slug |
| Ceramic Mug | Ceramic-Mug |
| Desk Lamp | Desk-Lamp |
| Linen Throw | Linen-Throw |
SQL
SELECT lower(replace(replace(name, ' ', '-'), '.', '')) AS url_slug
FROM products
WHERE id = 2;
Result 1 row
| url_slug |
| learning-sql-step-by-step |
Functions nest: the inner call's result becomes the outer call's input.
Joining strings
SQL
SELECT concat_ws(', ', name, city, country) AS address_line
FROM customers
WHERE id IN (1, 6);
Result 2 rows
| address_line |
| Ana Pereira, Lisbon, Portugal |
| Farah Haddad, France |
concat_ws (concatenate with separator) puts the separator between values and skips NULLs — notice Farah's line has no gap for the missing city. It's available in SQLite, PostgreSQL, MySQL and SQL Server.
Common functions across databases
| Task | SQLite | PostgreSQL | MySQL | SQL Server |
| Length | length | length | char_length | len |
| Substring | substr | substring | substring | substring |
| Find position | instr | position / strpos | locate / instr | charindex |
| Concatenate | || | || | concat | + / concat |
| Left/right n chars | substr | left / right | left / right | left / right |
| Pad | format | lpad / rpad | lpad / rpad | format / replicate |
Try it yourself
Build a staff directory line like "WEBER, Jonas — Senior Developer" for every employee.