String Functions in SQL
CONCAT, SUBSTRING, TRIM, LOWER — text without app round-trips
Compute text near the data: || (standard) or CONCAT(a,b) joins, SUBSTRING(x FROM 2 FOR 3) slices, TRIM/LTRIM/RTRIM strips, LOWER/UPPER normalizes, LENGTH counts, REPLACE/OVERLAY rewrites. Case-insensitive compare wants LOWER(col) + expression index — or a CITEXT/collation column. Functional predicates without matching indexes scan.
Concat near data
|| standard or CONCAT() — display labels without app trips.
Normalize + strip
LOWER/UPPER for compare; TRIM for dirty input; REPLACE canonicalizes.
Slice + measure
SUBSTRING/LEFT prefixes; LENGTH validations and buckets.
Index the function
LOWER(email) predicates need expression indexes or they scan.
1SELECT name, LOWER(name) AS low, LENGTH(name) AS len, SUBSTRING(name FROM 1 FOR 3) AS pre FROM employees ORDER BY name LIMIT 3;
| name | low | len | pre |
|---|---|---|---|
| Alice | alice | 5 | Ali |
| Bob | bob | 3 | Bob |
| Cynthia | cynthia | 7 | Cyn |
Each row computes three text derivations in one pass. LOWER powers case-insensitive ORDER BY; LENGTH feeds validations; prefix feeds autocomplete buckets.
Display labels + lowers
Three derivations in one pass.
1SELECT name, LOWER(name) AS low, LENGTH(name) AS len FROM employees ORDER BY name LIMIT 2;
| name | low | len |
|---|---|---|
| Alice | alice | 5 |
| Bob | bob | 3 |
Case-insensitive login (fast)
Functional predicate made sargable.
1CREATE INDEX idx_lower_email ON users (LOWER(email));2SELECT * FROM users WHERE LOWER(email) = LOWER('ADA@X.COM');
| how | |
|---|---|
| ada@x.com | found via expression index |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.