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 | 3

Uppercase and length of each name.

Example 2 (sql)
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
Output
Ana Rao
Ben Lee

Combine 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.

๐Ÿ“ Quick Quiz

1. MySQL concatenation:

2. Standard SQL concatenation:

3. Which removes surrounding whitespace?