Skip to content
SQLSimplified
string

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

NameDescriptionOptional
stringThe text to trim.No
charsCharacters 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. TRIM only touches the ends; use REPLACE to remove characters inside a string.
  • Confusing with LTRIM/RTRIM. Those trim only one side and use a different argument style.

See also: LTRIM, RTRIM, REPLACE.

Search

Search lessons, functions, examples, and practice problems