Solved

EXAMPLE OF TRANSLATE

Posted on 2004-08-11
5
2,836 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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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…
This video shows how to recover a database from a user managed backup
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now