Skip to content

LTRIM, RTRIM

Remove leading or trailing space characters, or other specified characters, from the start or end of a string.

LTRIM(string [ ,string ])
RTRIM(string [ ,string ])

where the first argument is the character string that you want to trim.

The second, optional argument is a list of characters that you want to trim from the start (LTRIM) or end (RTRIM) of the first string. If you do not specify the second argument, the default behavior is to trim leading or trailing space characters.

The individual characters listed in the second argument are removed without respect to the order in which they are specified. For example, ytic will remove the string city. The listed characters are case-sensitive. For example, ytic will not remove the C from City, but it will remove ity.

Examples

The following examples trim leading and trailing spaces by default:

premdb=# select ltrim('    City') from sys.const;
 ltrim 
-------
 City
(1 row)

premdb=# select rtrim('    City     ') from sys.const;
  rtrim   
----------
    City
(1 row)

The following example trims the characters ytic from team names that end with City. Note that the capital C of City is not trimmed.

premdb=# select name, rtrim(name, 'ytic') from team where name like '%City' order by teamid;
     name       |    rtrim     
-----------------+--------------
 Birmingham City | Birmingham C
 Bradford City   | Bradford C
 Cardiff City    | Cardiff C
 Coventry City   | Coventry C
 Hull City       | Hull C
 Leicester City  | Leicester C
 Manchester City | Manchester C
 Norwich City    | Norwich C
 Stoke City      | Stoke C
 Swansea City    | Swansea C
(10 rows)

The following example trims the characters yticC from team names that end with City. Note that the capital C of City is now trimmed.

premdb=# select name, rtrim(name, 'yticC') from team where name like '%City' order by teamid;
     name       |    rtrim    
-----------------+-------------
 Birmingham City | Birmingham 
 Bradford City   | Bradford 
 Cardiff City    | Cardiff 
 Coventry City   | Coventry 
 Hull City       | Hull 
 Leicester City  | Leicester 
 Manchester City | Manchester 
 Norwich City    | Norwich 
 Stoke City      | Stoke 
 Swansea City    | Swansea 
(10 rows)

Parent topic:String Functions