Remove leading and/or trailing characters (typically whitespace) from a string.

Removes leading and/or trailing characters from a string, by default whitespace. Useful for cleaning user input, normalizing keys, and handling delimited data. Can also trim specific characters: TRIM(LEADING 0 FROM column).

Compatibility

Filter by Database
SQL TRIM Compatibility Across Databases
Database System Support Status Since Version Notes
MySQL Native all TRIM([LEADING|TRAILING|BOTH] [remstr FROM] str). Removes whitespace by default. LTRIM() and RTRIM() for one-sided trim. Can trim specific characters: TRIM(BOTH 'x' FROM 'xxhelloxx').
PostgreSQL Native all TRIM([LEADING|TRAILING|BOTH] [chars FROM] str). Also LTRIM(str [, chars]) and RTRIM(str [, chars]) which accept a set of characters to strip (not just whitespace). btrim() = TRIM(BOTH ...).
SQL Server Native all LTRIM() and RTRIM() have always been available. TRIM() (both sides, whitespace) added in SQL Server 2017. TRIM() with a character set argument added in SQL Server 2022.
Oracle Native all TRIM([LEADING|TRAILING|BOTH] [char FROM] str). LTRIM(str [, chars]) and RTRIM(str [, chars]) also available and accept character sets.
SQLite Native all TRIM(str [, chars]), LTRIM(str [, chars]), RTRIM(str [, chars]). The optional chars argument is a set of characters to strip, not a substring.

Details

SQL Server is the notable laggard — TRIM() for both sides only arrived in 2017 (use LTRIM(RTRIM()) before that). PostgreSQL and Oracle's LTRIM/RTRIM accept a character *set* argument, not a substring — LTRIM('abcfoo', 'abc') strips any leading a, b, or c characters, not the literal substring 'abc'.

Standard Syntax

SELECT TRIM(' hello '); -- 'hello' SELECT LTRIM(' hello'); -- 'hello ' SELECT RTRIM('hello '); -- ' hello'

Version Support

MySQL: Native in all listed versions PostgreSQL: Native in all listed versions SQL Server: Native in all listed versions Oracle: Native in all listed versions SQLite: Native in all listed versions

Per-Database Syntax & Notes

MySQL Native syntax

TRIM([LEADING|TRAILING|BOTH] [remstr FROM] str). Removes whitespace by default. LTRIM() and RTRIM() for one-sided trim. Can trim specific characters: TRIM(BOTH 'x' FROM 'xxhelloxx').

SELECT TRIM(' hello '); -- 'hello' SELECT TRIM(BOTH 'x' FROM 'xxhelloxx'); -- 'hello' SELECT LTRIM(' hello'); -- 'hello' SELECT RTRIM('hello '); -- 'hello'

PostgreSQL Native syntax

TRIM([LEADING|TRAILING|BOTH] [chars FROM] str). Also LTRIM(str [, chars]) and RTRIM(str [, chars]) which accept a set of characters to strip (not just whitespace). btrim() = TRIM(BOTH ...).

SELECT TRIM(' hello '); -- 'hello' SELECT TRIM(BOTH 'x' FROM 'xxhelloxx'); -- 'hello' SELECT LTRIM('xxxhello', 'x'); -- 'hello' SELECT BTRIM('xxhelloxx', 'x'); -- 'hello'

SQL Server Native syntax

LTRIM() and RTRIM() have always been available. TRIM() (both sides, whitespace) added in SQL Server 2017. TRIM() with a character set argument added in SQL Server 2022.

SELECT LTRIM(RTRIM(' hello ')); -- pre-2017 both-side trim SELECT TRIM(' hello '); -- 2017+ SELECT TRIM('x' FROM 'xxhelloxx'); -- 2022+ (character argument)

Oracle Native syntax

TRIM([LEADING|TRAILING|BOTH] [char FROM] str). LTRIM(str [, chars]) and RTRIM(str [, chars]) also available and accept character sets.

SELECT TRIM(' hello ') FROM DUAL; -- 'hello' SELECT TRIM(BOTH 'x' FROM 'xxhelloxx') FROM DUAL; -- 'hello' SELECT LTRIM('xxxhello', 'x') FROM DUAL; -- 'hello'

SQLite Native syntax

TRIM(str [, chars]), LTRIM(str [, chars]), RTRIM(str [, chars]). The optional chars argument is a set of characters to strip, not a substring.

SELECT TRIM(' hello '); -- 'hello' SELECT TRIM('xxhelloxx', 'x'); -- 'hello' SELECT LTRIM(' hello'); -- 'hello'