PostgreSQL – LTRIM Function

The ltrim function is used to remove characters from the left end (beginning) of a string. It takes two arguments: the input string and a list of characters to be removed from the left side of the input string.

Here’s the syntax for the ltrim function:

ltrim(input_string, characters_to_remove)

where

  • input_string is the string from which you want to remove characters from the left side.
  • characters_to_remove(optional) is a string containing the characters you want to remove from the left side of the input string. If you do not provide a string, ltrim function will remove the nulls from the left side.

The ltrim function will return a new string with all leading characters from the characters_to_remove list removed from the left side of the input string.

Here’s an example of how to use the ltrim function:

SELECT ltrim('Hello, world!', 'H');

In this example, the ltrim function is used to remove ‘H’ from the left side of the string 'Hello, world!'. The result of this query will be 'ello, world!' because it removed the leading ‘H’.

You can use any list of characters in the characters_to_remove argument, and ltrim will remove all occurrences of those characters from the left side of the input string until it encounters a character that is not in the list.

LTRIM function can be used to remove the leading null characters as below:

SELECT ltrim('   Hello, world!', ' '); -- removes leading nulls

or

SELECT ltrim(' Hello, world!'); -- removes leading nulls

Similar Posts