string
TRIM
Removes matching characters from the start, end, or both sides of a string. Defaults to stripping spaces when no character set is supplied.
TRIM([LEADING|TRAILING|BOTH] [chars] FROM string) | TRIM(string) Parameters / Modifiers
| Parameter | Purpose |
|---|---|
| LEADING | Strip only from the left side |
| TRAILING | Strip only from the right side |
| BOTH | Default: strip from both ends |
| LTRIM / RTRIM | Shortcut functions available in every dialect |
Examples
SELECT TRIM(' hello '); Returns 'hello'
SELECT TRIM(BOTH '/' FROM '/api/v1/'); Returns 'api/v1'
SELECT LTRIM(RTRIM(username)) FROM users; Portable double-sided trim in every dialect
SELECT TRIM(LEADING '0' FROM '000042'); Returns '42' — strip leading zeros
Dialect notes / Gotcha
TRIM() only removes spaces by default, not tabs or newlines — pass an explicit character set to remove whitespace. SQLite TRIM only accepts single-character sets, not multi-char strings like Postgres/MySQL.
Related functions
REPLACE
Replaces every occurrence of from_str inside source with to_str. Case-sensitive in Postgres and SQLite; MySQL follows the column's collation.
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.
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.
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.
LOWER
Converts a string to lowercase using the database's active collation/locale. Universally supported and safe for normalizing case-insensitive lookups.
← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet