TRIM()
Removes leading and/or trailing characters (usually spaces) from a string.
Description
TRIM strips unwanted characters from the edges of a string. By default it
removes spaces from both ends (BOTH), which is the most common use — cleaning
up user input or fixed-width text. You can also target just LEADING (left) or
TRAILING (right) edges, and specify which character to remove.
Syntax
TRIM(string)
TRIM(BOTH FROM string)
TRIM(LEADING FROM string)
TRIM(TRAILING 'x' FROM string)Parameters
| Name | Description | Optional |
|---|---|---|
| string | The text to trim. | No |
| chars | Characters to remove (default space). | Yes |
Return Type
Returns a VARCHAR.
Examples
Employees & Departments
Loading database engine...
Employees & Departments
Loading database engine...
Trim specific characters
TRIM(BOTH '0' FROM '00123') returns 123. Without specifying, only spaces
are removed.
Common Mistakes
- Trimming only spaces when you meant a character. Specify the character
explicitly, e.g.
TRIM(TRAILING '.' FROM col). - Expecting to trim the middle.
TRIMonly touches the ends; useREPLACEto remove characters inside a string. - Confusing with LTRIM/RTRIM. Those trim only one side and use a different argument style.
Related Functions
See also: LTRIM, RTRIM, REPLACE.