string
LEFT / RIGHT
LEFT returns the first n characters, RIGHT returns the last n characters. Available in Postgres and MySQL. SQLite has no LEFT/RIGHT — use SUBSTR(s, 1, n) for LEFT and SUBSTR(s, -n) for RIGHT.35+; a portable fallback is SUBSTRING(str, 1, n) or SUBSTRING(str, LENGTH(str)-n+1).
LEFT(string, n) | RIGHT(string, n) Parameters / Modifiers
| Parameter | Purpose |
|---|---|
| n < 0 | Postgres: return all except the last/first |n| characters |
| SUBSTRING(str, 1, n) | Portable equivalent of LEFT for older engines |
| RIGHT(str, n) | Mirror function returning the trailing n characters |
Examples
SELECT LEFT('hello world', 5); Returns 'hello'
SELECT RIGHT('hello world', 5); Returns 'world'
SELECT LEFT(sku, 3), RIGHT(sku, 3) FROM products; Extract 3-letter prefix and suffix from each SKU
SELECT RIGHT(phone, 4) FROM customers; Last four digits of phone numbers
Dialect notes / Gotcha
SQLite has no LEFT/RIGHT functions; emulate with SUBSTR(str, 1, n) for LEFT and SUBSTR(str, -n) for RIGHT. Postgres and MySQL support both natively.
Related functions
SUBSTRING
Extracts a substring by 1-based start position and optional length. All three dialects support the comma-argument form; Postgres also accepts the SQL-standard FROM/FOR clauses and POSIX regex extraction.
LENGTH / CHAR_LENGTH
Returns the number of characters in a string (CHAR_LENGTH) or the number of bytes (LENGTH in MySQL). Postgres and SQLite LENGTH return character count for text, matching CHAR_LENGTH.
SPLIT_PART
Splits a string on a delimiter and returns the Nth part (1-based). Native to Postgres; MySQL emulates via SUBSTRING_INDEX and SQLite requires application-side splitting.
CONCAT
Joins two or more strings end-to-end and returns a single string. CONCAT is portable across MySQL, Postgres 9.1+, and SQLite 3.44+; the standard || operator works in Postgres and SQLite but not MySQL unless PIPES_AS_CONCAT is set.
REPLACE
Replaces every occurrence of from_str inside source with to_str. Case-sensitive in Postgres and SQLite; MySQL follows the column's collation.
← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet