PostgreSQL translate() Function
The PostgreSQL translate() function translates a specified string according to the specified translation relation. A translation relation is a one-to-one correspondence between characters in two strings.
translate() Syntax
This is the syntax of the PostgreSQL translate() function:
translate(string, from_set, to_set)
Parameters
string-
Required. The string to translate.
from_set-
Required. The string including characters to translate.
to_set-
Required. The string that is the set of characters to translate to. The characters in
from_setand the characters into_setform a one-to-one translation relationship according the order of characters.
Return value
The PostgreSQL translate() function returns a translated string string with all characters specified in from_set be translated to the corresponding characters in to_set.
translate() Examples
This example shows how to use the translate() function to translate a string:
SELECT translate('xabcdef', 'abcd', '123');
translate
-----------
x123efLet’s take a look at the executing steps of translate('xabcdef', 'abcd', '123'):
-
from_setisabcd. It tells us the four characters (a,b,candd) are going to be translated. -
to_setis123. The following translation relationships betweenfrom_setandto_setare established:awill be translated into1bwill be translated into2cwill be translated into3dwill be translated to'', that is the empty string
-
The translation process is as follows:
xis not infrom_set, soxis reserved.ais infrom_set, soais translated into1.bis infrom_set, sobis translated into2.cis infrom_set, socis translated into3.dis infrom_set, sodis translated into''.eis not infrom_set, soeis reserved.fis not infrom_set, sofis reserved.
-
The result of the translation was
x123efandtranslate()returned it.