SQL ยท Chapter 24 of 42
String Functions
Common string functions: `UPPER`, `LOWER`, `LENGTH`, `SUBSTRING`, `TRIM`, `CONCAT`, `REPLACE`.
Syntax varies slightly across databases.
Concatenation
Standard SQL uses `||`. MySQL uses `CONCAT()`. SQL Server uses `+`.
Extraction
`SUBSTRING(str, start, length)` extracts part of a string (1-based index in most DBs).
Example 1 (sql)
SELECT UPPER(name), LENGTH(name) FROM users;Output
ANA | 3
BEN | 3Uppercase and length of each name.
Example 2 (sql)
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;Output
Ana Rao
Ben LeeCombine two columns.
Key points
- UPPER, LOWER change case.
- LENGTH returns character count.
- SUBSTRING extracts part of a string.
- TRIM removes surrounding whitespace.
๐ก Note: For safe search, do `WHERE LOWER(name) = LOWER('Ana')` OR add a functional index โ otherwise the LOWER() disables the index.
