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_set
and the characters into_set
form 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
-----------
x123ef
Let’s take a look at the executing steps of translate('xabcdef', 'abcd', '123')
:
-
from_set
isabcd
. It tells us the four characters (a
,b
,c
andd
) are going to be translated. -
to_set
is123
. The following translation relationships betweenfrom_set
andto_set
are established:a
will be translated into1
b
will be translated into2
c
will be translated into3
d
will be translated to''
, that is the empty string
-
The translation process is as follows:
x
is not infrom_set
, sox
is reserved.a
is infrom_set
, soa
is translated into1
.b
is infrom_set
, sob
is translated into2
.c
is infrom_set
, soc
is translated into3
.d
is infrom_set
, sod
is translated into''
.e
is not infrom_set
, soe
is reserved.f
is not infrom_set
, sof
is reserved.
-
The result of the translation was
x123ef
andtranslate()
returned it.