Using the REPLACE Function in PostgreSQL

Updated: January 6, 2024 By: Guest Contributor Post a comment

Overview

The REPLACE function in PostgreSQL is a string manipulation tool that allows you to replace all occurrences of a specified substring with another substring within a given string. By using this function, developers can easily and quickly modify strings without the need for complex programming constructs.

Origins of REPLACE in PostgreSQL

The REPLACE function has been a part of PostgreSQL for many years, aligning with the SQL standard. It’s included in most if not all versions where you would expect full SQL string function support.

Use

The purpose of the REPLACE function in PostgreSQL is to search for a specified string within another string and replace it with a different string. This is particularly useful for data cleaning tasks, updating string formats, or preparing text data before analysis.

Syntax and Parameters

Syntax:

REPLACE(original_string, substring_to_replace, replacement_substring)

where:

  • original_string is the string in which you want to replace the substring.
  • substring_to_replace is the substring that you want to search for within the original_string.
  • replacement_substring is the substring that will replace each occurrence of substring_to_replace.

The function returns a new string with all instances of the substring_to_replace replaced by the replacement_substring.

Code Examples

Example 1: Simple Replacement

In this example, we demonstrate a basic replacement where we change ‘cat’ to ‘dog’ in a given string.

-- Example query replacing 'cat' with 'dog'
SELECT REPLACE('The cat sat on the mat', 'cat', 'dog') AS new_string;

The output of this simple SELECT query will be ‘The dog sat on the mat’.

Example 2: Update Table Values

This example shows how REPLACE can be used to update values within a table. Imagine you have a table ‘products’ with a column ‘product_name’, and you need to replace the word ‘Old’ with ‘New’ in the product names.

-- Updating 'Old' with 'New' in product names
UPDATE products
SET product_name = REPLACE(product_name, 'Old', 'New')
WHERE product_name LIKE '%Old%';

After executing this update command, all product names containing ‘Old’ will be updated to ‘New’.

Example 3: Removing Substrings

The REPLACE function can also be used to remove a certain substring from the original string by replacing it with an empty string.

-- Remove all occurrences of 'remove' from the string
SELECT REPLACE('Please remove this word: remove', 'remove', '') AS new_string;

The result will be ‘Please this word: ‘. Note that the space before ‘remove’ is retained in the result.

Conclusion

The REPLACE function in PostgreSQL is a powerful and versatile tool for string manipulation. It simplifies tasks that would otherwise require more complex functions or stored procedures. As seen in the examples provided, REPLACE can be used for a variety of text processing tasks, from simple replacements to more complex data sanitization and update operations. Understanding and utilizing the REPLACE function can greatly benefit anyone working with textual data in PostgreSQL.