?
Solved

EXAMPLE OF TRANSLATE

Posted on 2004-08-11
5
Medium Priority
?
2,841 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
[X]
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
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 75 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 150 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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

777 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