PostgreSQL ltrim() Function
The PostgreSQL ltrim()
function removes the longest string containing only characters specified by the argument (space by default) from the start of a string.
You can also use rtrim()
to remove specified characters from the end of a string, or use btrim()
to remove specified characters from both ends of a string.
ltrim()
Syntax
This is the syntax of the PostgreSQL ltrim()
function:
ltrim(text)
or
ltrim(text, characters)
Parameters
text
-
Required. The string to remove characters.
characters
-
Optional. The string containing the characters to remove. The default is a space.
Return value
The PostgreSQL ltrim()
function removes the longest string containing only characters specified by the argument (one space by default) from the start of a string, and returns the string.
ltrim()
Examples
SELECT
length(ltrim('a ')) AS "length(ltrim('a '))",
length(ltrim(' a')) AS "length(ltrim(' a'))",
length(ltrim(' a ')) AS "length(ltrim(' a '))";
length(ltrim('a ')) | length(ltrim(' a')) | length(ltrim(' a '))
----------------------+----------------------+----------------------
3 | 1 | 2
Here:
- We’ve only used one parameter, then
ltrim()
will remove spaces from start of the string. - To make the result look more intuitive, we use
length()
function to display length of the returned string.
Let’s use ltrim()
to remove specified characters from start of a string:
SELECT ltrim('xxyHELLOzxy', 'xyz') AS "ltrim('xxyHELLOzxy', 'xyz')";
ltrim('xxyHELLOzxy', 'xyz')
-----------------------------
HELLOzxy