Oracle String/Char Function - Oracle/PLSQL TRIM Function






This Oracle tutorial explains how to use the Oracle/PLSQL TRIM function.

The Oracle/PLSQL TRIM function removes all specified characters either from the beginning or the ending of a string.

Syntax

TRIM removes a set of characters from both sides of a string. The general format for this function is:

TRIM ([where] [trim_character] FROM subject string)

The optional where is one of the keywords "leading", "trailing", or "both". If the optional trim_character is not present, then blanks will be trimmed. The word FROM is necessary only if where or trim_character is present. Trim character may be any character.





Leading and trailing spaces

Both the leading and trailing spaces are deleted.


SQL> SELECT TRIM (' abc   ') FROM dual;

TRI
---
abc

SQL>

We can add the "both" keyword:


SQL> SELECT TRIM (both ' ' from ' abc   ') FROM dual;

TRI
---
abc

SQL>




Non space

In the following examples, characters rather than spaces are trimmed:


SQL> SELECT TRIM('F' from 'Friday is a Day') FROM dual;
-- w w  w.jav  a 2 s.  com
TRIM('F'FROM'F
--------------
riday is a Day

SQL> SELECT TRIM(trailing 's' from 'Cars') FROM dual;

TRI
---
Car

SQL>

leading

Using "leading" keyword.


SQL> SELECT TRIM(leading 'F' from 'Friday Father Further') FROM dual;
-- from  ww  w  .  j  ava 2 s  .  c om
TRIM(LEADING'F'FROM'
--------------------
riday Father Further

SQL>

Nesting TRIM functions:

SQL> SELECT TRIM(trim(both 'a' from 'a abc')) FROM dual;

TRI
---
abc

SQL>