GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Oracle  >  SQL

 Print  |  
Question:  Replace and Translate

Answer: How and in what way REPLACE is different from TRANSLATE?


July 07, 2009 07:22:37 #6
 Rohit_Ash   Member Since: May 2009    Total Comments: 6 

RE: Replace and Translate
 
replace( STRING, string_to_replace, replacement_string) replaces string_to_replace  by  replacement_string as a whole.

All Bs would be replaced by O.
 
SQL> select replace('ABC PBR XBZ','B','O') from dual;
REPLACE('AB
-----------
AOC POR XOZ

There wont be any change since APX string is not there.

SQL> select replace('ABC PBR XBZ','APX','MNO') from dual;
REPLACE('AB
-----------
ABC PBR XBZ

Translate(STRING, string_to_replace, replacement_string) replaces the 1st character in the string_to_replace with the 1st character in the replacement_string. Then it will replace the 2nd character in the string_to_replace with the 2nd character in the replacement_string, and so on.

All Bs would be replaced by Os.

SQL> select translate('ABC PBR XBZ','B','O') from dual;
TRANSLATE('
-----------
AOC POR XOZ

All As would be replaced by M, all Ps would be replaced by N and all Os would be replaced by Os.

SQL> select translate('ABC PBR XBZ','APX','MNO') from dual;
TRANSLATE('
-----------
MBC NBR OBZ
     

 

Back To Question