Search notes:

Oracle SQL functions: RTRIM, LTRIM, TRIM

TRIM(string) removes a given character or characters of a given set from both side of a text (string), LTRIM(string) only from the string's left side, RTRIM(string) only from the string's right side.
If the character to be removed is not explicitly specified, it defaults to the space.
None of these functions remove the character (or space) from within the string.

Examples

Remove spaces

create table tq84_trim_test (
   txt  varchar2(20)
);

insert into tq84_trim_test values ('   foo    ');
insert into tq84_trim_test values ('  bar   baz   ');

select
   '>' ||  trim(txt) || '<'  "trim",
   '>' || ltrim(txt) || '<' "ltrim",
   '>' || rtrim(txt) || '<' "rtrim"
from
   tq84_trim_test;
--
--  trim                   ltrim                  rtrim                 
--  ---------------------- ---------------------- ----------------------
--  >foo<                  >foo    <              >   foo<              
--  >bar   baz<            >bar   baz   <         >  bar   baz<         

Remove a specified character

The following example removes stars from either end of the strings
select
   '>'  ||
      trim ('*' from '*** removing stars * at both sides ***')
   || '<'
           "Stars, but not spaces removed"
  from dual;
--
-- Stars, but not spaces removed     
-- ----------------------------------
-- > removing stars * at both sides <

Remove multiple characters

The following query removes the characters in the string ({<#!$./"?>}) from the left side of #!$Hello World.:
select
   LTRIM('#!$Hello World.', '({<#!$./"?>})') xyz
from
   dual;
--
-- XYZ         
-- ------------
-- Hello World.

See also

Functions related to text and strings

Index