EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL TRIM()
 

PostgreSQL TRIM()

Aanchal Singh
Article byAanchal Singh
EDUCBA
Reviewed byRavi Rathore

Updated May 10, 2023

PostgreSQL TRIM()

 

 

Definition of PostgreSQL TRIM()

PostgreSQL TRIM() function is used to eradicate spaces or a particular set of characters that may be leading, trailing, or present on both sides of the string. The trim() function can remove the longest string, which can have characters in the beginning, in the end, or both at the beginning and the end. By default, the trim() function removes spaces unless explicitly instructed to remove specific characters. This string can be of the data type char, varchar, or any text.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

Trim( [ leading | trailing | both ] [ trim character ] from string )

This is the basic syntax of the trim function. In this case, the trim() function is accompanied by options such as leading, trailing, and trim characters. Let us have a look at these in detail. The above syntax can be defer in the following ways.

  • Trim(Leading from a string): This will remove the spaces which are present at the beginning of the string.
  • Trim (Trailing from a string): This will remove the spaces which are present at the end of the string.
  • Trim (Both from a string): This will remove the spaces present at the beginning and end of the string. You can also use the Trim () function by simply specifying the string as Trim (string). This is also the default way in which the trim function works.

How does PostgreSQL TRIM() Function Work?

Trim works in the above-specified variations. Let us have a look at each of these in detail.

  • Trim (leading from a string): Using this function, the spaces or characters which are present at the starting of a given string can be removed.
select TRIM(LEADING FROM '     This is removing leading spaces');

In this case, the trim() function removes the spaces at the beginning of the string.

PostgreSQL TRIM()-1.1

Here leading is the keyword. It removes all spaces which are present at the start.

  • Trim (Trailing from a string): Using this function, the spaces or characters which are present at the end of a given string can be removed.
select TRIM(Trailing FROM 'This is removing trailing spaces           ');

PostgreSQL TRIM()-1.2

Here the spaces at the end of the string are removed. Trailing is the keyword.

  • Trim (Both from a string): Using this function, the spaces or characters which are present at the end of a given string can be removed.
select TRIM(Both FROM '           This is removing trailing spaces           ');

PostgreSQL TRIM()-1.3

Here the spaces from both ends of the string are removed, and Both is the keyword.

Examples to TRIM() Function in PostgreSQL

Let us have a look at different examples of the TRIM function.

  • We can easily remove unwanted characters by making use of the trim function.
SELECT TRIM('00 ' FROM '    00  AAA   ') AS TrimmedString;

PostgreSQL TRIM()-2.1

In this example, we are trimming the extra 0’s which are present in the text. The text is ’00  AAA ‘. We are removing the extra spaces and 00, which are present at the beginning of the string. The resulting string is being stored in the variable named “TrimmedString”. Once you run this code, it produces the output shown in the screenshot. It gives only ‘AAA’. The declared variable stores the string with the extra 0’s and spaces removed.

  • The RTRIM functions in a similar way to LTRIM. It removes the trailing spaces or characters which are present in the string.
SELECT RTRIM('AAA TTT     ') AS TrimmedString;

Output-2.2

  • The third is the default way which removes spaces from both sides of the string.
SELECT TRIM(Both '00 ' FROM ' 00   AAA  000 ') AS NewString;

Output-2.3

This function removes both blank spaces and ’00’ from either end of the string. The given string has spaces at both the start and end of the string’ 00   AAA 00 ‘. Using the trim() function as is successfully removes white spaces from both ends of the string, resulting in the output ‘AAA’. The trim() function removes any unnecessary white spaces from the string.

SELECT TRIM(Trailing '00 ' FROM '         AAA    000  ') AS NewString;

Output-2.4

Here the TRIM function trims the extra spaces and ’00’, which are at the end of the string or the spaces trailing after AAA. It removes them and then outputs the data without the spaces. This output string is then stored in the declared variable. You can observe that spaces at the beginning areas it is as we have not used the Leading Trim function here.

The trim function can also be used with an array. We can make use of the procedural query. By using declare, we can initialize a variable, and then in a while loop or loop, we can use the trim function as and when needed.

Advantages of using TRIM() Functions in PostgreSQL

The trim() function has advantages as below, which makes it easy and efficient to use. Some of them are listed below:

  • This is the easiest and simplest of the string function, which is present in SQL. You can easily use this for any string, and as a result, we can eliminate unnecessary spaces or characters that are unnecessary.
  • When creating lengthy scripts that are not feasible to eliminate spaces from a particular string, it is efficient to use the Trim () function, which removes all spaces from the string.
  • It not only works for spaces, but the user can also remove unwanted characters.
  • The trim() function can be used in various contexts, including in conjunction with arrays and stored procedures, or on its own to remove spaces from a string. It has multiple purposes.
  • Since it is a built-in function, using the trim() function can enhance the performance of a script or procedure by quickly carrying out its task.
  • It provides variations like removing only spaces from the beginning of the string or the string’s end.

Conclusion

The Trim () function’s versatility makes it useful in SQL scripts of varying complexity. It provides quick results with enhanced performance. All modern-day tools used in data warehouses and data cleansing support this function. All these capabilities make this function very important, and that is why people use it extensively.

Recommended Articles

We hope that this EDUCBA information on “PostgreSQL TRIM()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. PostgreSQL NOT NULL
  2. Notify PostgreSQL
  3. PostgreSQL UNIQUE Constraint
  4. Guide to PostgreSQL Queries

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW