PostgreSQL: translate Function
This PostgreSQL tutorial 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('Techonthenet.com', 't', 'A'); translate ------------------ TechonAheneA.com (1 row) postgres=# SELECT translate('Techonthenet.com', 'met', 'ABC'); translate ------------------ TBchonChBnBC.coA (1 row) postgres=# SELECT translate('Techonthenet.com', 'e.t', '456'); translate ------------------ T4chon6h4n465com (1 row)
Advertisements