Skip to Main Content
  • Questions
  • Remove special character chr(191) and others from Oracle 9i

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, abhi.

Asked: January 13, 2016 - 4:21 am UTC

Last updated: January 13, 2016 - 5:58 am UTC

Version: 9iR2

Viewed 10K+ times! This question is

You Asked

I have a field which gets populated with incorrect characters and specially the chr(191) ' inverted ? sign'..

Original txt : . Unit 1 has stated, “I’M SO SORRY”. XXXX unable to obtain a full version from Unit 1 due to her injuries and QAS request that

Converted text : has stated, ¿I¿M SO SORRY¿. XXXX unable to obtain a full version from Unit 1 due So, ideally i do want to see the original text,

but for 9i version i cannot seem to get any equivalent functions as used by 10g or higher.


I had this sample code written but for 10g versions :-

UPDATE rc.rc_descripton
SET desc_description =
(
CASE
WHEN desc_description LIKE '%[^a-zA-Z0-9]%'
THEN Replace(REPLACE( desc_description, SUBSTRING( desc_description, regexp_instr('%[~,@,#,$,%,&,*,^,&,%,*,(,)]%', desc_description), 1 ),''),'-',' ')
ELSE desc_description
END
)

Tried using OWA_PATTERN.AMATCH but certainly does not work for 9i version too.

So what function can i use to make the above script work for oracle 9ir2 versions...??

and Connor said...

Without rolling your own version, perhaps something like CONVERT

SELECT replace(CONVERT('Ä Ê Í Õ Ø A B C D E ', 'US7ASCII', 'WE8ISO8859P1'),'?')    FROM DUAL; 


or use ASCIISTR to replace "bad" data with \xxx, and then hunt for that

declare
  x varchar2(100) := 'Hello Ä Ê ÍTHere';
  y varchar2(200) := asciistr(x);
  res varchar2(100); 
begin
  while instr(y,'\') > 0 loop
     res := res || substr(y,1,instr(y,'\')-1);
     y := substr(y,instr(y,'\')+5);
  end loop;
  dbms_output.put_line(res||y);
end;
/


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

using convert function in 9i

abhi mehta, January 13, 2016 - 5:14 am UTC

select desc_description,
  length(dbms_lob.substr(desc_description, 3500, 1)) len,
  lengthb(dbms_lob.substr(desc_description, 3500, 1)) lenb,

  convert(replace(translate(dbms_lob.substr(desc_description, 3500, 1), ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=`~!@#$%^&*()_+,./<>?;'':"[]\{}|',' '), ' ', ''),'us7ascii') bad_char_replaced,

  rawtohex(convert(replace(translate(dbms_lob.substr(desc_description, 3500, 1), ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-=`~!@#$%^&*()_+,./<>?;'':"[]\{}|',' '), ' ', ''),'us7ascii')) bad_char_replaced_hex ,
  -------
  convert(dbms_lob.substr(desc_description, 3500, 1),'us7ascii') converted
  -------
  from rc.rc_description

  WHERE  desc_description LIKE '%' || CHR(191) || '%' and
  desc_cras_number = '20140096849';


The before text is : vehicle�s B pillar has ......

The After text is : vehiclei??s B pillar has ......

Can I get the above query to remove these ??? to apostrophe or double quotes ? or if that screws the data then jst use an empty space.
Connor McDonald
January 13, 2016 - 5:58 am UTC

Apologies - I misread your question to be *remove* the characters, rather than re-capture them.

If you are getting upside down question mark, there are 3 things that need to capable of handling the special characters they represent:

1) the database characterset
2) the client characterset
3) the client program/OS

For example, in SQL Plus on Windows (which runs in CMD prompt), no matter what you, some characters cannot be displayed, but CMD simply cant do it.

In the case above, where (I'm guessing) it was the common 'smart quotes' issue, and you want to preserve them - you need to ensure that your db and client NLS can handle it.

If you want to ditch them, then see my first answer.

If you want selectively replace them, then you need to a series of replace/translate as you have done, but using the code point number for smart quotes, eg

select ascii(' <smartquote> ') from dual;

and then

replace( string, chr(<number from above>), '')



More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here