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
nameshoutingquiet
The Patient GardenerTHE PATIENT GARDENERthe patient gardener
Wireless EarbudsWIRELESS EARBUDSwireless 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
namecharacters
Learning SQL Step by Step25
The Patient Gardener20
Mechanical Keyboard19

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
emailusernamedomain
ana.pereira@example.comana.pereiraexample.com
ben.carter@example.comben.carterexample.com
chloe.martin@example.comchloe.martinexample.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_nameshort_name
EthanE. Walsh
PriyaP. Nair

Trimming and padding

SQL
SELECT '[' || trim('   spaced out   ') || ']' AS trimmed,
       '[' || ltrim('   left') || ']' AS left_trimmed,
       trim('--note--', '-') AS custom;
Result 1 row
trimmedleft_trimmedcustom
[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_refpadded
00003[delivered ]
00012[pending ]

Replacing text

SQL
SELECT name, replace(name, ' ', '-') AS slug
FROM products
WHERE category = 'Home';
Result 3 rows
nameslug
Ceramic MugCeramic-Mug
Desk LampDesk-Lamp
Linen ThrowLinen-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

TaskSQLitePostgreSQLMySQLSQL Server
Lengthlengthlengthchar_lengthlen
Substringsubstrsubstringsubstringsubstring
Find positioninstrposition / strposlocate / instrcharindex
Concatenate||||concat+ / concat
Left/right n charssubstrleft / rightleft / rightleft / right
Padformatlpad / rpadlpad / rpadformat / replicate

Try it yourself

Build a staff directory line like "WEBER, Jonas — Senior Developer" for every employee.

Try it yourself
Result