EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL TRIM()

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL TRIM()

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 be present on both sides of the string. The trim() function can remove the longest string, which can have characters in the beginning, at the end or both at the beginning and the end. This function’s default functionality is removing spaces if there is no explicit mention of any characters that are to be removed. This string can be of the data type char, varchar or any text.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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

This is the basic syntax of the trim function. Here the trim function is followed by options like leading, trailing, trim characters. Let us have a look at these in detail. The above syntax can 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 which are present at the beginning and at the end of the string. This can also be done by using the function as simply mentioning 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');

Here the spaces at the start of the string are removed.

PostgreSQL TRIM()-1.1

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

Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (2,974 ratings)
Course Price

View Course

Related Courses
  • 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 output here is being stored in TrimmedString. The screenshot below shows the output that we get once this code is run. It gives only ‘AAA’. The extra 0’s and spaces are removed from the string and stored in the declared variable.

  • 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 was the function when blank spaces and ‘00’are removed from both ends of the string. The given string has spaces present at both the start and at the end of the string ‘    00   AAA 00       ‘. Due to this, when the trim function is used as it is, it does its job of removing the white spaces from both ends of the string, and the output that we get is as: ‘AAA’. All unnecessary blank or white spaces are removed 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 present at the end of the string or the spaces which are 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 get rid of unnecessary spaces or characters which are not needed.
  • When creating scripts that are lengthy and 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 remove unwanted characters as well.
  • It can be used along with arrays, stored procedures, or simply to remove any spaces. It has multiple purposes.
  • It is quick and enhances the performance of the script or procedure where it is being used as this is a built-in function.
  • It provides variations like removing only spaces from the beginning of the string or the string’s end.

Conclusion

The trim() function, as a result, is very helpful and can be used in a simple to complex SQL scripts. It provides quick results with enhanced performance. This function is supported by all modern-day tools which are used in data warehouses and data cleansing. All these capabilities of this function make it very important and also is the reason why it is used extensively.

Recommended Articles

This is a guide to PostgreSQL TRIM(). Here we also discuss the Introduction and how the postgresql trim() function works, along with different examples and its code implementation. You may also have a look at the following articles to learn more –

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

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

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

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More