PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Functions / PostgreSQL TRIM Function

PostgreSQL TRIM Function

Summary: in this tutorial, you will learn how to use PostgreSQL TRIM function to remove the longest string that contains spaces or a particular character from a string.

Introduction to PostgreSQL TRIM function

The TRIM function removes the longest string that contains a specific character from a string. By default, the TRIM function remove spaces (‘ ‘) if you don’t specify explicitly which character that you want to remove.

With the TRIM function, you can remove a longest string containing a character from the start, end, or both start and end of a string. Note that a string can be any of the following data types: char, varchar, and text.

The TRIM function is very useful when we want to remove the unwanted characters from string in the database. We often use it for data cleansing.

The following illustrates the syntax of the TRIM function.

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

For example, if you want to remove spaces from the beginning of a string, you use the following syntax:

1
TRIM(LEADING FROM string)

The following syntax of the TRIM function removes all spaces from the end of a string.

1
TRIM(TRAILING FROM string)

And to remove all spaces at both beginning and ending of a string, you use the following syntax:

1
TRIM(BOTH FROM string)

Or just simply:

1
TRIM(string)

PostgreSQL TRIM function examples

See the following examples of removing leading, trailing, and both leading and trailing spaces from strings.

1
2
3
4
5
6
7
8
9
10
11
12
SELECT
TRIM (
LEADING
FROM
'  PostgreSQL TRIM'
),
TRIM (
TRAILING
FROM
'PostgreSQL TRIM   '
),
TRIM ('  PostgreSQL TRIM  ');

PostgreSQL TRIM function examples

The following statement updates the first_name and last_name columns of the customer table in the sample database with the values that do not have leading and trailing spaces. It uses the TRIM function to remove both leading and trailing spaces from the first_name and last_name columns.

1
2
3
UPDATE customer
SET first_name = TRIM (first_name),
    last_name = TRIM (last_name);

The following statement removes leading zero (0) from a number. Because the  TRIM function only accepts a string as the argument, we have to use type cast to convert the number into a string before passing it to the TRIM function.

1
2
3
4
5
6
SELECT
TRIM (
LEADING '0'
FROM
CAST (0009100 AS TEXT)
); -- 9100

PostgreSQL LTRIM, RTRIM, and BTRIM functions

PostgreSQL provides you with LTRIM, RTRIM and BTRIM functions that are the shorter version of the TRIM function.

  • The LTRIM function removes all characters, spaces by default, from the beginning of a string.
  • The RTRIM function removes all characters, spaces by default, from the end of a string.
  • The BTRIM function is the combination of LTRIM and RTRIM functions.

The syntax of LTRIM and RTRIM function are as follows:

1
2
3
LTRIM(string, [character]);
RTRIM(string, [character]);
BTRIM(string, [character]);

This is equivalent to the following syntax of the TRIM function:

1
2
3
TRIM(LEADING character FROM string); -- LTRIM(string,character)
TRIM(TRAILING character FROM string); -- RTRIM(string,character)
TRIM(BOTH character FROM string); -- BTRIM(string,character)

Let’s take a look at the following examples of using LTRIM, RTRIM , and BTRIM functions to remove the character e from the enterprise string:

1
2
SELECT
LTRIM('enterprise', 'e');

PostgreSQL LTRIM example

1
2
SELECT
RTRIM('enterprise', 'e');

PostgreSQL RTRIM example

1
2
SELECT
BTRIM('enterprise', 'e');

PostgreSQL BTRIM example

Removing whitespace characters from a string

Sometimes, your string may contain whitespace characters such as space, tab, line break, etc., that you want to remove. However, the TRIM function only allows you to remove leading and trailing spaces, not all other whitespace characters. You can call the TRIM function multiple times but it is not efficient.

One way to remove the leading and trailing whitespace characters from a string is to use REGEXP_REPLACE function. For example, the following statement remove a space and a tab character from the end of the enterprise string with 1 space and 1 tab characters at the end of the string.

1
SELECT REGEXP_REPLACE('enterprise ', '\s+$', '');

The \s+$ is explained as follows:

  • \s : regular expression class shorthand for whitespace.
  • + means 1 or more consecutive matches.
  • $  meas end of string.

You use the ^\s+ regular expression if you want to remove leading whitespace characters.

In this tutorial, we have shown you how to use TRIM, LTRIM, RTRIM, and BTRIM functions to remove characters from the beginning, ending, and both beginning and ending of a string.

Previous Tutorial: PostgreSQL LENGTH Function
Next Tutorial: PostgreSQL CONCAT Function

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.