SQL > SQL String Functions > Trim

The TRIM function in SQL is used to remove a specified prefix or suffix from a string. The most common pattern being removed is the white space. This function is called differently in different databases:

  • MySQL: TRIM( ), RTRIM( ), LTRIM( )
  • Oracle: RTRIM( ), LTRIM( )
  • SQL Server: RTRIM( ), LTRIM( )
Key Takeaway: SQL provides TRIM, LTRIM, and RTRIM functions to remove unwanted leading and trailing whitespace or characters from strings. MySQL supports all three, while Oracle and SQL Server only support LTRIM and RTRIM.

Syntax

The syntax for the TRIM function is as follows:

TRIM( [ [LOCATION] [remstr] FROM ] str)

[LOCATION] can be either LEADING, TRAILING, or BOTH. This function gets rid of the [remstr] pattern from either the beginning of the string or the end of the string, or both. If no [remstr] is specified, white spaces are removed.

The syntax for the LTRIM function is as follows:

LTRIM (str)

LTRIM removes all white spaces from the beginning of the string.

The syntax for the RTRIM function is as follows:

RTRIM (str)

RTRIM removes all white spaces at the end of the string.

Examples

Example 1: TRIM function

SELECT TRIM('   Sample   ');

Result:

'Sample'

Example 2: LTRIM function

SELECT LTRIM('   Sample   ');

Result:

'Sample   '

Example 3: RTRIM function

SELECT RTRIM('   Sample   ');

Result:

'   Sample'

Frequently Asked Questions

What does the SQL TRIM function do?
The SQL TRIM function removes a specified prefix, suffix, or both from a string. When no removal pattern is given, it strips leading and trailing white spaces by default.
What is the difference between LTRIM and RTRIM?
LTRIM removes white spaces from the left (beginning) of a string, while RTRIM removes white spaces from the right (end) of a string. TRIM can do both at once.
Can TRIM remove characters other than spaces?
Yes, in MySQL the TRIM function accepts a LEADING, TRAILING, or BOTH keyword with a custom remstr argument to strip specific characters from the string.
Is the TRIM function available in all SQL databases?
Support varies by database. MySQL supports TRIM(), LTRIM(), and RTRIM(). Oracle and SQL Server only natively support LTRIM() and RTRIM(). The ANSI SQL standard defines TRIM() with LEADING/TRAILING/BOTH options.

Next: SQL Length

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact