PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • String Functions
    • Aggregate Functions
Home / PostgreSQL Functions / PostgreSQL TRANSLATE

PostgreSQL TRANSLATE

The PostgreSQL TRANSLATE() function performs several single-character, one-to-one translation in one operation.

Syntax

The following illustrates the syntax of the PostgreSQL TRANSLATE() function:

1
TRANSLATE(string, from, to)

Arguments

The PostgreSQL TRANSLATE() function accepts three arguments:

1) string
is a string subjected to translation.

2) from
is a set of characters in the first argument (string) that should be replaced.

3) to
is a set of characters that replaces the from in the string.

Notice that if from is longer than to, the TRANSLATE() function removes the occurrences of the extra characters in from.

Return value

The PostgreSQL TRANSLATE() function returns a string with the characters in the one set of characters are replaced by another set of characters.

Examples

Let’s see some examples of using the TRANSLATE() function to understand how it works.

A) Simple TRANSLATE() example

See the following statement:

1
SELECT TRANSLATE('12345', '134', 'ax')

In this example:

  • The character '1' in string '12345' is substituted by character 'a', The character '3' in the string '12345' is substituted by the character 'x'.
  • Because the string '134' has more characters than the string 'ax', the TRANSLATE() function removes the extra character in the string '134', which is '4', from the string '12345'.

The following illustrates the result:

1
2
3
4
translate
-----------
a2x5
(1 row)

B) Single character replacement

The following example shows how to use the TRANSLATE() function to replace comma (,) by semi-colon (;) in a comma-separated values list.

1
SELECT TRANSLATE('apple,orange,banana', ',', ';');

Here is the output:

1
2
3
4
  translate
---------------------
apple;orange;banana
(1 row)

C) Encrypting and decrypting a message

The following example shows how to use the TRANSLATE() function to encrypt a message:

1
2
3
SELECT TRANSLATE('a secret message',
                 'abcdefghijklmnopqrstuvxyz',
                 '0123456789acwrvyuiopkjhbq');

Here is the output:

1
2
3
4
    translate
------------------
0 o42i4p w4oo064
(1 row)

You can also decrypt the message '0 o42i4p w4oo064' using the function:

1
2
3
SELECT TRANSLATE('0 o42i4p w4oo064',
                     '0123456789acwrvyuiopkjhbq',
                     'abcdefghijklmnopqrstuvxyz');

Hence the output is:

1
2
3
4
    translate
------------------
a secret message
(1 row)

In this tutorial, you have learned how to use the PostgreSQL TRANSLATE() function to substitute characters in a set with another, one-to-one, in a single operation.

Previous Tutorial: PostgreSQL CONCAT Function
Next Tutorial: PostgreSQL AGE Function

PostgreSQL String Functions

  • ASCII
  • CONCAT
  • CHR
  • FORMAT
  • LEFT
  • LENGTH
  • LPAD
  • MD5
  • POSITION
  • REGEXP_MATCHES
  • REGEXP_REPLACE
  • RIGHT
  • REPLACE
  • SPLIT_PART
  • SUBSTRING
  • TRANSLATE
  • TRIM

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL String Functions
  • PostgreSQL SPLIT_PART
  • PostgreSQL CHR
  • PostgreSQL ASCII
  • An Overview of PostgreSQL Aggregate Functions
  • PostgreSQL POSITION
  • PostgreSQL MD5
  • PostgreSQL REGEXP_MATCHES
  • PostgreSQL REGEXP_REPLACE
  • PostgreSQL FORMAT

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.