Oracle TRANSLATE() Function
Oracle TRANSLATE() is a built-in function that translates a given string based on a specified translation relationship and returns the translated string.
Oracle TRANSLATE() Syntax
Here is the syntax for the Oracle TRANSLATE() function:
TRANSLATE(string, from_string, to_string)
Parameters
string-
Required. The string to be translated.
from_set-
Required. A string that is a set of characters to be translated.
to_set-
Required. A string that is the set of characters to be translated to. Characters in
to_setcorrespond one-to-one to characters infrom_setto form the translation relationship.
Return Value
The Oracle TRANSLATE() function returns a translated string that translates all characters specified in from_set in string to the corresponding characters specified in to_set.
If any of the parameters is NULL, TRANSLATE() returns NULL.
Oracle TRANSLATE() Examples
Here are some examples that demonstrate the usage of the Oracle TRANSLATE() function.
Basic Usage
This example shows how to use the Oracle TRANSLATE() function to translate a string:
SELECT
TRANSLATE('xabcdef', 'abcd', '123')
AS "TRANSLATE('xabcdef', 'abcd', '123')"
FROM dual;
Output:
TRANSLATE('xabcdef', 'abcd', '123')
______________________________________
x123efLet’s take a look at the execution process of TRANSLATE('xabcdef', 'abcd', '123'):
-
from_setisabcd, which tells us that the four charactersa,b,c,dare to be translated. -
to_setis123, andfrom_setandto_setestablish the following translation relationship:ais translated to1bis translated to2cis translated to3dis translated to'', i.e. the empty string
-
The translation process for the string
'xabcdef'is as follows:xis not infrom_set, so it is retained.ais infrom_set, so it is translated to1.bis infrom_set, so it is translated to2.cis infrom_set, so it is translated to3.dis infrom_set, so it is translated to''.eis not infrom_set, so it is retained.fis not infrom_set, so it is retained.
-
The result of the translation is:
x123ef.
NULL Parameters
If any of the parameters is NULL, TRANSLATE() returns NULL.
SET NULL 'NULL';
SELECT
TRANSLATE(NULL, 'A', 'B') Result1,
TRANSLATE('A', NULL, 'B') Result2,
TRANSLATE('A', 'B', NULL) Result3
FROM dual;
Output:
RESULT1 RESULT2 RESULT3
__________ __________ __________
NULL NULL NULLIn this example, we use the statement SET NULL 'NULL'; to display NULL values as the string 'NULL'.
Conclusion
Oracle TRANSLATE() is a built-in function that translates a given string according to a given translation relationship and returns the translated string.