w3resource

PostgreSQL LTRIM() function

LTRIM() function

The PostgreSQL ltrim() function is used to remove spaces ( if no character(s) is provided as trimming_text ) or set of characters which are matching with the trimming_text, from the start of a string.

It is useful for cleaning up or formatting strings by removing unwanted characters from the start.

Uses of LTRIM() Function
  • Remove Leading Spaces: Clean up strings by eliminating leading white spaces.

  • Custom Trimming: Remove specified characters from the start of a string.

  • Data Formatting: Prepare data for consistent formatting by removing unwanted characters from the beginning.

  • String Cleanup: Process and clean data entries that may contain leading extraneous characters.

Syntax:

ltrim(<string>[, <triming_text>]) 

PostgreSQL Version: 9.3

Pictorial Presentation of PostgreSQL LTRIM() function

Pictorial presentation of PostgreSQL LTRIM() function

Example: PostgreSQL ltrim() function:

In the example below, the statement returns the result 'ltrim'. Here in the example the trimming_text is provided, and it is 'best', and the left four characters i.e. 'test' have removed and the characters 't' and 'e' and 's' are present in the trimming_text 'best'.

Code:

SELECT ltrim('testltrim', 'best');

Sample Output:

 ltrim
-------
 ltrim
(1 row)

Example of PostgreSQL ltrim() function with no text:

In the example below, the trimming_text is not provided, so by default the white spaces from the left side of the given string will be removed.

Code:

SELECT ltrim('     ltrim');

Sample Output:

 ltrim
-------
 ltrim
(1 row)

Previous: LPAD function
Next: PG_CLIENT_ENCODING function



Follow us on Facebook and Twitter for latest update.