Postgresql Translate Function

PostgreSQL: translate Function

In this PostgreSQL post explains how to use the PostgreSQL translate function with syntax and examples.

Description

The PostgreSQL translate function replaces a sequence of characters in a string with another set of characters. However, it replaces a single character at a time.

For example, it will replace the 1st character in the string_to_replace with the 1st character in the replacement_string. Then it will replace the 2nd character in the string_to_replace with the 2nd character in the replacement_string, and so on.

Syntax

The syntax for the translate function in PostgreSQL is:

translate( string1, string_to_replace, replacement_string )

Parameters or Arguments

string1

The string to replace a sequence of characters with another set of characters.

string_to_replace

The string that will be searched for in string1.

replacement_string

All characters in the string_to_replace will be replaced with the corresponding character in the replacement_string.

Applies To

The translate 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 translate function examples and explore how to use the translate function in PostgreSQL.

For example:

postgres=# SELECT translate('AODBA.com', 'O', 'A');
    translate
------------------

AADBA.com
(1 row)

postgres=# SELECT translate('AODBA.com', 'BA', 'AB');
    translate
------------------

AODAB.com
(1 row)