Thursday, January 10, 2013

Ltrim Function


In Oracle/PLSQL, the ltrim function removes all specified characters from the left-hand side of a string.

Syntax

The syntax for the ltrim function is:
ltrim( string1, [ trim_string ] )
string1 is the string to trim the characters from the left-hand side.
trim_string is the string that will be removed from the left-hand side of string1. If this parameter is omitted, the ltrim function will remove all leading spaces from string1.

Applies To

  • Oracle 11g, Oracle 10g, Oracle 9i, Oracle 8i

For Example

ltrim('   tech');would return 'tech'
ltrim('   tech', ' ');would return 'tech'
ltrim('000123', '0');would return '123'
ltrim('123123Tech', '123');would return 'Tech'
ltrim('123123Tech123', '123');would return 'Tech123'
ltrim('xyxzyyyTech', 'xyz');would return 'Tech'
ltrim('6372Tech', '0123456789');would return 'Tech'
The ltrim function may appear to remove patterns, but this is not the case as demonstrated in the following example.
ltrim('xxyyxzyxyyxTech', 'xyz');would return 'Tech'
It actually removes the individual occurrences of 'x', 'y', and 'z', as opposed to the pattern of 'xyz'.
The ltrim function can also be used to remove all leading numbers as demonstrated in the next example.
ltrim( '637Tech', '0123456789');would return 'Tech'
In this example, every number combination from 0 to 9 has been listed in the trim_string parameter. By doing this, it does not matter the order that the numbers appear in string1, all leading numbers will be removed by the ltrim function.