SQL TRIM(), LTRIM(), RTRIM() Functions

SQL TRIM(), LTRIM(), RTRIM() functions trim the char of the string.

SQL TRIM() Function

SQL TRIM() function remove all specified trim char from beginning and ending of the string.

Supported Oracle SQL Version

  • Oracle 8i
  • Oracle 9i
  • Oracle 10g
  • Oracle 11g
  • Oracle 12c
  • Oracle 18c

Syntax

TRIM( 
    [ 
        [ LEADING | TRAILING | BOTH ] 
        trim_character FROM 
    ] string 
)

Parameters

  • LEADING remove trim_character from beginning of the string.
  • TRAILING remove trim_character from ending of the string.
  • BOTH remove trim_character from both beginning and ending of the string.
  • trim_character specified the trim character that you want to remove from the string. If you not specify TRIM function remove spaces from string.
  • FROM is a keyword.
  • string original string that we want to trim.

Example

Consider following example how to TRIM function remove specified character.

SQL> SELECT TRIM(LEADING 'N' FROM 'NOpal Kole') "LEADING TRIM" FROM DUAL;

LEADING TRIM
------------
   Opal Kole

SQL LTRIM() Function

SQL LTRIM() function remove all specified trim char from left side of the string.

Supported Oracle SQL Version

  • Oracle 8i
  • Oracle 9i
  • Oracle 10g
  • Oracle 11g
  • Oracle 12c
  • Oracle 18c

Syntax

LTRIM(string, trim_char)

Parameters

  • string is string that string you want to trim from left side.
  • trim_char is specified char that character you want remove from the string.

Example

Consider following example remove all specified trim char from left side of the given string argument.

SQL> SELECT LTRIM('2254Opal Kole', '2254') "LTRIM" FROM DUAL;

LTRIM
---------
Opal Kole

SQL RTRIM() Function

SQL RTRIM() function remove all specified trim char from right side of the string.

Supported Oracle SQL Version

  • Oracle 8i
  • Oracle 9i
  • Oracle 10g
  • Oracle 11g
  • Oracle 12c
  • Oracle 18c

Syntax

RTRIM(string, trim_char)

Parameters

  • string is original string.
  • trim_char is specified char that character you want remove from the string.

Example

Consider following example remove all specified trim char from right side of the given string argument.

SQL> SELECT RTRIM('Opal Kole2254', '2254') "RTRIM" FROM DUAL;

RTRIM
---------
Opal Kole