PLSQL | TRANSLATE Function

The PLSQL TRANSLATE function is used for replacing a sequence of characters in a string with another set of characters. The PLSQL TRANSLATE function replaces a single character at a time. The TRANSLATE function replaces the first character of the input_string with the first character of the replacement_string and then the second character and follows the same flow for the remaining characters.

The TRANSLATE function accepts three parameters input_string, string_to_replace, replacement_string. If a character appears multiple times in string_to_replace, then the replacement_string mapping corresponding to the first occurrence is used. The TRANSLATE function returns a string value.

Syntax:



TRANSLATE( input_string, string_to_replace, replacement_string )

Parameters Used:

  1. input_string – It is used to specify the source string.
  2. string_to_replace – It is used to specify the string that will be searched for in the input_string.
  3. replacement_string – It is used to specify the characters which will be replaced with the corresponding character in the input_string.

Return Value:
The TRANSLATE function in PLSQL returns a string value.

Supported Versions of Oracle/PLSQL:

  1. Oracle 12c
  2. Oracle 11g
  3. Oracle 10g
  4. Oracle 9i
  5. Oracle 8i

Example-1: Passing all the three parameters to the TRANSLATE function to replace consecutive letters from the input_string.

DECLARE 
   Test_String string(25) := 'Giiksforgiiks';
   
BEGIN 
   dbms_output.put_line(TRANSLATE(Test_String, 'ii', 'ee')); 
   
END;     

Output:

Geeksforgeeks 

Example-2: Passing all the three parameters to the TRANSLATE function to replace non-consecutive letters from the input_string.

DECLARE 
   Test_String string(25) := 'Geeksforgeeks';
   
BEGIN 
   dbms_output.put_line(TRANSLATE(Test_String, 'Gkrs', 'abcd')); 
   
END; 

Output:

aeebdfocgeebd 

Example-3: Passing all the three parameters to the TRANSLATE function to replace uppercase and lowercase letters from the input_string.

DECLARE 
   Test_String string(25) := 'Geeksforgeeks';
   
BEGIN 
   dbms_output.put_line(TRANSLATE(Test_String, 'Gg', 'Aa')); 
   
END;  

Output:

Aeeksforaeeks 

Advantage:

  • REPLACE can be used to substitute a single string for another single string, as well as remove character strings.
  • TRANSLATE can be used to make several single-character, one-to-one substitutions in one operation.


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.