SQL text functions: UPPER, LENGTH, SUBSTR and ||
Text functions reshape text in your result. The data in the table does not change.
SQL skills · Functions
What you will learn
Names typed in different ways, codes made from the first letters, labels built from two columns: text functions handle them.
The ideas are the same everywhere; some names differ by database.
The basics
Text functions reshape text in your result. The data in the table does not change.
SELECT UPPER(customer), LOWER(customer), LENGTH(customer),
SUBSTR(customer, 1, 3), customer || ' (' || region || ')'
FROM invoicesNames in other databases
LENGTH is LEN in SQL Server. SUBSTR is SUBSTRING in SQL Server and also works in PostgreSQL. || joins text in PostgreSQL, Oracle, SQLite and Snowflake; SQL Server uses + or CONCAT(), and SQL Server 2025 adds ||.
Watch out for NULL
In most databases 'Ava' || NULL is NULL. Wrap columns that may be empty in COALESCE(column, '').
Worked examples
Example 1
A display label for each product.
SELECT product || ' (' || warehouse || ')' AS label
FROM shipments|| joins text in most databases.
Reading helps, typing makes it stick. Every task gives you a fresh table, and your query is checked by a real database.
Try a taskCommon mistakes
- Starting SUBSTR at 0. Counting starts at 1.
- Joining text with a NULL column, which makes the whole result NULL.
- Using LENGTH in SQL Server, where it is LEN.
Where you will use it at work
- HR: email style logins from names.
- Operations: short product codes.
- Marketing: clean campaign names before matching.
Questions people ask
How do I join text in SQL Server?
With + or CONCAT(). SQL Server 2025 also accepts ||.
How do I trim spaces?
TRIM(text) removes spaces at both ends.
How do I replace part of a text?
REPLACE(text, 'old', 'new').
Ready to write it yourself?
Practice Text functions