PostgreSQL - TRIM Function
Last Updated :
11 Nov, 2024
The TRIM() function in PostgreSQL is an essential tool for removing unwanted characters from strings. Whether we're working with user inputs, formatting text, or performing data cleansing operations, TRIM() is an invaluable function for managing string data.
This article will provide an in-depth look at how to use the TRIM() function, including syntax, examples, and practical use cases while focusing on SEO-optimized keywords for PostgreSQL string functions and data manipulation.
PostgreSQL TRIM Function
The TRIM() function is used to remove specified characters from the start, end, or both ends of a string. By default, TRIM() removes spaces, but we can specify any character we would like to remove. This makes it highly flexible for cleaning up text and formatting data for easier manipulation or presentation.
In PostgreSQL, the TRIM() function is commonly used in scenarios like removing unnecessary spaces from user inputs, cleansing raw data before inserting it into tables, or formatting text for better readability.
Syntax
TRIM([LEADING | TRAILING | BOTH] [characters] FROM string)
Key Terms
- LEADING: Removes the specified characters from the beginning of the string.
- TRAILING: Removes characters from the end of the string.
- BOTH: Removes characters from both the beginning and end of the string.
- characters (optional): The characters you want to remove. If not specified, it removes spaces by default.
- string: The input string from which you want to remove characters.
Key Points
- Default Behavior: If no specific characters are mentioned, the
TRIM()
function removes spaces by default.
- Removing Specific Characters: You can specify a character or a set of characters to be removed.
- Trim Direction: We can specify whether to trim characters from the start (
LEADING
), end (TRAILING
), or both ends (BOTH
) of the string.
PostgreSQL TRIM Function Examples
Let us take a look at some of the examples of the TRIM() Function in PostgreSQL to better understand the concept, including various use cases such as removing spaces, leading zeros, and handling complex data formatting.
Example 1: Removing Leading, Trailing, and Both Leading and Trailing Spaces
The following statement removes leading, trailing, and both leading and trailing spaces from strings, ensuring that any unwanted white spaces are cleaned up for consistent data formatting.
Query:
SELECT
TRIM(LEADING FROM ' Geeks ForGeeks') AS leading_trimmed,
TRIM(TRAILING FROM 'Geeks ForGeeks ') AS trailing_trimmed,
TRIM(' Geeks ForGeeks ') AS both_trimmed;
Output

Explanation:
leading_trimmed
: 'Geeks ForGeeks'
(removes leading spaces)
trailing_trimmed
: 'Geeks ForGeeks'
(removes trailing spaces)
both_trimmed
: 'Geeks ForGeeks'
(removes both leading and trailing spaces)
Example 2: Removing Leading Zeros from a Number
The following statement removes the leading zero (0) from a number. As the function only accepts string values we have to use a type cast to convert the number into a string before passing it to the TRIM() function.
Query:
SELECT
TRIM(LEADING '0' FROM CAST(0009100 AS TEXT)) AS trimmed_number;
Output

Explanation:
The TRIM() function removes the leading zeros from the number string, resulting in "9100."
Example 3: Combining TRIM() with Other String Functions
We can use TRIM() in combination with other functions like UPPER() or LOWER() to clean and format text simultaneously.
Query:
SELECT
TRIM(BOTH FROM UPPER(' PostgreSQL Database ')) AS formatted_text;
Output
formatted_text |
---|
POSTGRESQL DATABASE |
Explanation:
This query first converts the string to uppercase and then removes spaces from both ends
Important Points About PostgreSQL TRIM() Function
- The
TRIM()
function can be combined with other string functions for more complex data manipulations. For example, combining TRIM()
with UPPER()
to trim spaces and convert a string to uppercase.
- When performing data cleansing on a large dataset, the
TRIM()
function can be used in an UPDATE
statement to clean up the data.
- When no specific character is specified, the
TRIM()
function removes white spaces by default.
- The
TRIM()
function can be used on numbers and other data types by first converting them to strings.
Conclusion
In conclusion, the TRIM() function in PostgreSQL is a powerful tool for removing unwanted characters from strings, making it an essential part of our data cleansing toolkit. Whether we're dealing with whitespace issues, formatting text for readability, or cleaning raw data for analysis, TRIM() allows us to easily manage string data and improve our database's integrity.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
Top 60 DBMS Interview Questions with Answers for 2025 A Database Management System (DBMS) is the backbone of modern data storage and management. Understanding DBMS concepts is critical for anyone looking to work with databases. Whether you're preparing for your first job in database management or advancing in your career, being well-prepared for a DBMS
15+ min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read
SQL Views Views in SQL are a type of virtual table that simplifies how users interact with data across one or more tables. Unlike traditional tables, a view in SQL does not store data on disk; instead, it dynamically retrieves data based on a pre-defined query each time itâs accessed. SQL views are particular
7 min read
MySQL Tutorial This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to perform
11 min read
Indexing in Databases - Set 1 Indexing is a crucial technique used in databases to optimize data retrieval operations. It improves query performance by minimizing disk I/O operations, thus reducing the time it takes to locate and access data. Essentially, indexing allows the database management system (DBMS) to locate data more
8 min read