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
| 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
Version Support
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').
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 ...).
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.
Oracle Native syntax
TRIM([LEADING|TRAILING|BOTH] [char FROM] str). LTRIM(str [, chars]) and RTRIM(str [, chars]) also available and accept character sets.
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.