Oracle LTRIM()
function removes from the left-end of a string all characters contained in a set.
Syntax
The following illustrates the syntax of the Oracle LTRIM()
function:
LTRIM(trim_source,[set])
Code language: SQL (Structured Query Language) (sql)
Arguments
The LTRIM()
function accepts two arguments:
1) trim_source
is the string that unwanted characters should be removed.
2) set
is a set that contains one or more characters which should be removed from the trim_source
string.
The set
argument is optional. If you don’t specify it, the set will default to a single space.
The data types of both trim_source
and set
can be one of the following types CHAR
, VARCHAR2
, NCHAR
, NVARCHAR2
, CLOB
, or NCLOB
.
Return Value
The LTRIM()
function returns a string with all characters contained in the set are removed from the left end of the string.
The result string has the VARCHAR2
data type if the source_string
is a character value or LOB
if source_string
is a LOB
value.
Examples
The following example removes all spaces from the left end of a string ' XYZ'
:
SELECT
LTRIM( ' XYZ' )
FROM
dual;
Code language: SQL (Structured Query Language) (sql)
In this statement, we did not specify the character set that should be removed, the LTRIM()
function removed spaces by default.
Here is the result.
'XYZ'
Code language: SQL (Structured Query Language) (sql)
The following example illustrates how to remove characters in a set of (1,2,3) from the left end of the string 'XYZ123456'
SELECT
LTRIM( '123456XYZ', '123' )
FROM
dual;
Code language: SQL (Structured Query Language) (sql)
The result is:
456XYZ
Code language: SQL (Structured Query Language) (sql)
Let’s take a look at the following products
table in the sample database:
The following statement returns products whose names start with 'ASRock'
SELECT
product_name
FROM
products
WHERE
product_name LIKE 'ASRock%'
ORDER BY
product_name;
Code language: SQL (Structured Query Language) (sql)
You can use the LTRIM()
function to remove the 'ASRock'
string from the product names to make shorter as follows:
SELECT
product_name,
LTRIM( product_name, 'ASRock' ) short_product_name
FROM
products
WHERE
product_name LIKE 'ASRock%'
ORDER BY
product_name;
Code language: SQL (Structured Query Language) (sql)
In this tutorial, you have learned how to use the Oracle LTRIM()
function to remove unwanted characters from the left end of a string.