PostgreSQL TRIM() String Function



PostgreSQL TRIM() String Function

The PostgreSQL TRIM() function is used to remove the longest string consisting only of characters in a specified set from both ends of a string. This function is essential for cleaning up and standardizing text data.


Syntax

TRIM([LEADING | TRAILING | BOTH] [characters FROM] string)

The TRIM() function has the following components:

  • LEADING | TRAILING | BOTH: Specifies whether to trim characters from the start, end, or both ends of the string. Default is BOTH.
  • characters: The set of characters to be removed. Default is space.
  • string: The string to be trimmed.

Example PostgreSQL TRIM() Queries

Let's look at some examples of PostgreSQL TRIM() function queries:

1. Basic TRIM() Example

SELECT TRIM('  Hello, World!  ') AS trimmed_string;

This query removes spaces from both ends of the string ' Hello, World! ', resulting in 'Hello, World!'.

2. TRIM() with Specified Characters

SELECT TRIM('#' FROM '###Hello, World!###') AS trimmed_string;

This query removes hash signs from both ends of the string '###Hello, World!###', resulting in 'Hello, World!'.

3. TRIM() LEADING

SELECT TRIM(LEADING ' ' FROM '  Hello, World!  ') AS trimmed_string;

This query removes spaces from the start of the string ' Hello, World! ', resulting in 'Hello, World! '.

4. TRIM() TRAILING

SELECT TRIM(TRAILING ' ' FROM '  Hello, World!  ') AS trimmed_string;

This query removes spaces from the end of the string ' Hello, World! ', resulting in ' Hello, World!'.

5. TRIM() with Column Values

SELECT id, name, TRIM('*' FROM name) AS trimmed_name
FROM users;

This query retrieves the id and the trimmed name for each row in the users table, removing asterisks from both ends of the name.


Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the TRIM() function to clean up text data.

Step 1: Creating a Table

This step involves creating a new table named users to store user data.

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name TEXT
);

In this example, we create a table named users with columns for id and name.

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the users table.

INSERT INTO users (name)
VALUES ('  Alice  '),
       ('  Bob  '),
       ('  Charlie  ');

Here, we insert data into the users table.

Step 3: Using the TRIM() Function

This step involves using the TRIM() function to clean up the text data in the users table.

Basic TRIM()

SELECT TRIM('  Hello, World!  ') AS trimmed_string;

This query trims leading and trailing spaces from the string ' Hello, World! '.

TRIM() with Specified Characters

SELECT TRIM('#' FROM '###Hello, World!###') AS trimmed_string;

This query trims leading and trailing pound signs from the string '###Hello, World!###'.

TRIM() LEADING

SELECT TRIM(LEADING ' ' FROM '  Hello, World!  ') AS trimmed_string;

This query trims leading spaces from the string ' Hello, World! '.

TRIM() TRAILING

SELECT TRIM(TRAILING ' ' FROM '  Hello, World!  ') AS trimmed_string;

This query trims trailing spaces from the string ' Hello, World! '.

TRIM() with Column Values

SELECT id, name, TRIM(' ' FROM name) AS trimmed_name
FROM users;

This query trims leading and trailing spaces from the values in the 'name' column of the 'users' table.

These queries demonstrate how to use the TRIM() function to clean up text data in the users table, including basic usage and handling specified characters.

Conclusion

The PostgreSQL TRIM() function is a fundamental tool for cleaning up and standardizing text data by removing specified characters from both ends of a string. Understanding how to use the TRIM() function and its syntax is essential for effective text data manipulation in PostgreSQL databases.