totn PostgreSQL Functions

PostgreSQL: replace Function

This PostgreSQL tutorial explains how to use the PostgreSQL replace function with syntax and examples.

Description

The PostgreSQL replace function replaces all occurrences of a specified string.

Syntax

The syntax for the replace function in PostgreSQL is:

replace( string, from_substring, to_substring )

Parameters or Arguments

string
The source string.
from_substring
The substring to find. All occurrences of from_substring found within string are replaced with to_substring.
to_substring
The replacement substring. All occurrences of from_substring found within string are replaced with to_substring.

Note

  • The replace function performs a case-sensitive replacement.

Applies To

The replace function can be used in the following versions of PostgreSQL:

  • PostgreSQL 9.4, PostgreSQL 9.3, PostgreSQL 9.2, PostgreSQL 9.1, PostgreSQL 9.0, PostgreSQL 8.4

Example

Let's look at some PostgreSQL replace function examples and explore how to use the replace function in PostgreSQL.

For example:

postgres=# SELECT replace('techonthenet.com', 'techonthenet', 'checkyourmath');
      replace
-------------------
 checkyourmath.com
(1 row)

postgres=# SELECT replace('TechOnTheNet.com', 'techonthenet', 'checkyourmath');
      replace
-------------------
 TechOnTheNet.com
(1 row)

postgres=# SELECT replace('abc abc', 'a', 'B');
 replace
---------
 Bbc Bbc
(1 row)

postgres=# SELECT replace('abc abc', 'A', 'B');
 replace
---------
 abc abc
(1 row)