Loading lessons...
String Functions
String Functions
SQL has many functions for working with text.
UPPER and LOWER
SELECT UPPER(CustomerName) FROM Customers; -- "ALFREDS FUTTERKISTE"
SELECT LOWER(City) FROM Customers; -- "berlin"
LENGTH / LEN
MySQL uses LENGTH, SQL Server uses LEN:
SELECT LENGTH(City) FROM Customers; -- MySQL
SELECT LEN(City) FROM Customers; -- SQL Server
CONCAT
Combine strings:
SELECT CONCAT(FirstName, ' ', LastName) AS FullName
FROM Persons;
SUBSTRING / SUBSTR
Take part of a string:
SELECT SUBSTRING(City, 1, 3) FROM Customers; -- first 3 characters
TRIM
Remove spaces from both ends:
SELECT TRIM(' Hello ') AS Clean; -- 'Hello'
REPLACE
SELECT REPLACE('SQL Tutorial', 'SQL', 'MySQL'); -- 'MySQL Tutorial'
Note: function names vary
MySQL uses LENGTH; SQL Server uses LEN. Always check your database's reference.
TL;DR
- UPPER / LOWER change text case.
- CONCAT joins strings; LENGTH/LEN measures them.
- SUBSTRING cuts text; TRIM removes spaces; REPLACE swaps text.
- Function names differ slightly between databases.