TRIM function

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.

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>

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


SQL> SELECT TRIM('F' from 'Friday is a Day') FROM dual;

TRIM('F'FROM'F
--------------
riday is a Day

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

TRI
---
Car

SQL>

Using "leading" keyword.


SQL> SELECT TRIM(leading 'F' from 'Friday Father Further') FROM dual;

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>
Home »
Oracle »
String Functions » 

Related: