SQL Basics | Functions | Trim

The SQL function TRIM is used to remove part of the beginning or end of a string. Commonly, the function name differs by database.

  • MySQL: TRIM(), RTRIM(), LTRIM()
  • Oracle: RTRIM(), LTRIM()
  • SQL Server: RTRIM(), LTRIM()

Each trim function is as follows.

TRIM([[position] [remove_string] FROM] string)

The value entered for [position] is LEADING (beginning), TRAILING (end), or BOTH (beginning and end). This function removes [remove_string] from the beginning, end, or both beginning and end of the string. If [remove_string] is not specified, spaces are removed.

LTRIM (string)

Removes spaces from the beginning of the string.

RTRIM (string)

Removes spaces from the end of the string.

Trim example

TRIM() example

SELECT TRIM('   Sample   ');

The result is as follows.

'Sample'

LTRIM() example

SELECT LTRIM('   Sample   ');

The result is as follows.

'Sample   '

RTRIM() example

SELECT RTRIM ('   Sample   ');

The result is as follows.

'   Sample'