Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

EXAMPLE OF TRANSLATE

Posted on 2004-08-11
5
2,837 Views
Last Modified: 2010-08-05
I need an example of Oracle Translate function that strpits all psecial characters from the string and leavs only letters and numbers.
example translate '12-98*A-a(jj' to '1298Aajj'
0
Comment
Question by:smena
5 Comments
 
LVL 19

Expert Comment

by:grant300
ID: 11777833
You want the REPLACE function as opposed to the TRANSLATE function.

TRANSLATE does an ASCII code for ASCII code translation.

REPLACE will actually replace the charactors you don't want with nothing.

Example:

 v_string := REPLACE(v_string,'!@#$%^&*(){}[]\|`~.,/?/_=')

will strip all of the characters in the second argument from v_string.

If you have trouble getting all of the "special" characters into a quoted string, you might have to build a string variable containing all of the printable ones concatinated with CHR(xx)||CHR(yy)|| etc.  This is invariably easier than escaping quotes and other such nonsense.  You can wrap that definition as wiell as the REPLACE function into a user-defined function and call it whereever you need to.

Bill.
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 25 total points
ID: 11778086
Just put space ' ' plus the characters you want to remove in the first string and space ' ' in the secong string:

TRANSLATE(Col1, ' `~!@#$%^&*()_+={}[]|\":;<>?/', ' ')

0
 
LVL 5

Assisted Solution

by:mudumbe
mudumbe earned 50 total points
ID: 11779127
If you want to get rid of the blanks, you can use any character in the first position for eg:

TRANSLATE(Col1, 'x`~!@#$%^&*()_+={}[]|\":;<>?/', 'x')

This is because, the last 2 arguments to translate cannot be null.

You can also say:

TRANSLATE(Col1, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789')
0
 
LVL 19

Expert Comment

by:grant300
ID: 11779153
mudumbe:

I am pretty certain TRANSLATE is not exclusive.  That is, if you don't specify a character in the second argument, it won't be changed at all.

Bill
0
 
LVL 3

Assisted Solution

by:rkogelhe
rkogelhe earned 50 total points
ID: 11782892
Bill's right... but a more generic solution is to say:

translate(col1,'X'||translate(col1,'-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','-'),'X')

so in other words, remove all the good characters to find the bad ones, then use that as the parameter of the characters we want to remove.

SQL> select
  2  translate('12-98*A-a(jj' ,'X'||translate('12-98*A-a(jj' ,'-ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789','-'),'X')
  3  from dual;

TRANSLAT
--------
1298Aajj
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  â€¦
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question