PostgreSQL TRIM Function (original) (raw)

Last Updated : 15 Jul, 2025

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

**Key Points

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:

**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

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.