Open In App

PLSQL | TRANSLATE Function

Last Updated : 06 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads