PostgreSQL LTRIM() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL LTRIM()
function to remove specified characters from the beginning of a string.
Introduction to PostgreSQL LTRIM() function
The LTRIM()
function allows you to remove specified characters from the beginning of a string.
Here’s the syntax of the LTRIM()
function:
In this syntax:
string
is the input string that you want to remove characters.character
specifies the characters you want to remove from the beginning of thestring
. Thecharacter
parameter is optional. It defaults to space.
The LTRIM()
function returns the string with all leading characters removed.
To remove both leading and trailing characters from a string, you use the TRIM() function.
To remove the trailing characters from a string, you use the RTRIM() function.
PostgreSQL LTRIM() function examples
Let’s explore some examples of using the LTRIM()
function.
1) Basic PostgreSQL LTRIM() function example
The following example uses the LTRIM()
function to remove the #
from the beginning of the string #postgres
:
Output:
2) Using the PostgreSQL LTRIM() function to remove leading spaces
The following example uses the LTRIM()
function to remove all the spaces from the string ' PostgreSQL'
:
Output:
Since the default of the second argument of the LTRIM()
function is space, we don’t need to specify it.
3) Using the LTRIM() function with table data example
First, create a new table called articles
and insert some rows into it:
Output:
Second, update the titles by removing the leading spaces using the LTRIM()
function:
Output:
The output indicates that two rows were updated.
Third, verify the updates:
Output:
Summary
- Use
LTRIM()
function to remove all specified characters from the beginning of a string.