Basics
- How to Insert
- How to Update
- How to Delete
- How to Trim Strings
- How to Use substring()
- How to Use substring() with RegEx to Extract a String
- How to Replace Substrings
- How to Modify Arrays
- How to Compare Arrays
- How to Concatenate Strings
- How to Convert the Case of a String
- How to Create an Array
- How to Insert Data Into an Array
- How to Query Arrays
- How to Use string_agg()
Database Management
- How to Create a Table
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Change a Column Name
- How to Add a Default Value
- How to Remove a Default Value From a Column
- How to Add a Not Null Constraint
- How to Remove a Not Null Constraint
- How to Add an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
- How to Reset Sequence
- How to Drop a Column
Dates and Times
Analysis
- How to Use nullif()
- How to Use Lateral Joins
- How to Calculate Percentiles
- How to Get the First Row per Group
- How to Use generate_series to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV using Copy
- How to Compare Two Values When One Is Null
- How to Use Coalesce
- How to Write a Case Statement
- How to Use Filter to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Query a JSON Column
How to Trim Strings in PostgreSQL
When working with string data in PostgreSQL, you may often need to remove unnecessary spaces from the beginning or end of your strings. This is where the TRIM function comes in handy. In this tutorial, we'll explore how to use the TRIM function to clean up string values in your PostgreSQL database.
What is the TRIM Function?
The TRIM
function in PostgreSQL is used to remove specified prefixes or suffixes from a string. By default, it removes leading and trailing spaces.
Basic Syntax
The basic syntax for the TRIM function is as follows:
TRIM([LEADING | TRAILING | BOTH] [removal_character] FROM string)
LEADING
removes characters from the start of the string.TRAILING
removes characters from the end of the string.BOTH
removes characters from both ends of the string (this is the default).
Example 1: Removing Leading and Trailing Spaces
If you have a string with unnecessary spaces, you can use the TRIM
function to remove them:
SELECT TRIM(' hello world ');
This will return:
hello world
Example 2: Removing Specific Characters
You can also trim specific characters. For example, if you want to remove leading or trailing hyphens, you can do so with the following query:
SELECT TRIM(BOTH '-' FROM '--hello world--');
This will return:
hello world
Example 3: Trimming Only Leading or Trailing Characters
If you only want to remove characters from one side, you can use the LEADING
or TRAILING
option:
SELECT TRIM(LEADING '-' FROM '--hello world');
This will return:
hello world
Conclusion
The TRIM
function is a powerful and versatile tool for cleaning up string data in PostgreSQL. Whether you need to remove spaces, special characters, or both, the TRIM
function is your go-to solution. Try using it to clean your string data before performing any other operations or analyses on your database.