fixed record length *.csv oracle DB export, adjust column size

Hi experts
We're exporting oraclle table content in a fixed record length *.csv file.
SELECT
 'A' || nvl2(field1,rpad(field1,10,' '),lpad('?',144,' '))
 ||  nvl2(field2,rpad(field2,10,' '),lpad('?',40,' '))
 ||  nvl2(code1,rpad(MANDANTENCODE,5,' '),lpad('?',5,' ')) || 'B'
from table1;
The issue is the following, that some records have in field1 or field2 nonprintable characters for example ‎‚¿‘ HEX  BF
the effect is that this according column does not have the expected size.
example correct record
Asämple    sample2   33   B
wrong record where the column field1 is only 9 characters long instead of 10
As¿ple    sample2   33   B

seems that rpad fails with non expected characters

Thanks for a solution
mruffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flow01Commented:
For a long term solution  fix the data ,  if possible find the source of the pollution to avoid problems in the future,  validate/correct data before registration and (i asume you are using a database with a not UTF-8 characterset: considere moving that way).
Fix : search for the  ascii value :
select dump(field1)   from table1  where ... = record in error  -- to find the ascii value of the not printable character
with ascii value = ?nnn?

update table1
set field1 = replace(field1, chr(?nnn?),'?')
where field1 <> replace(field1, chr(?nnn?),'?')

You will probably find more then one ascii value in different records that causes the troubles  

For the short term you could try in this export
a. using the same replace (and possible more ascii values)  for field1 in the query
b.  compensate for the missing position  (did not check for myself if it works)
SELECT
 'A' || nvl2(field1,rpad(field1,10 + INSTR(field1,CHR(?nnn?),' '),lpad('?',144 ,' '))
 ||  nvl2(field2,rpad(field2,10,' '),lpad('?',40,' '))
 ||  nvl2(code1,rpad(MANDANTENCODE,5,' '),lpad('?',5,' ')) || 'B'
from table1;
0
slightwv (䄆 Netminder) Commented:
If the column has a hex BF in it, what do you want in the CSV output?
0
mruffAuthor Commented:
Hi Netminder

To hex BF has to be replaced the  by a blank, in other words just to be removed.
But it is important that the resulting column length is still 10 characters long
0
slightwv (䄆 Netminder) Commented:
Take a look at regular expression replace.

This should replace all non-printable characters with a space:
nvl2(field1,rpad(regexp_replace(field1,'[^[:print:]]',' ')),10,' '),lpad('?',144,' '))

This should do all non-alphanumeric:
nvl2(field1,rpad(regexp_replace(field1,'[^[:alnum:]]',' ')),10,' '),lpad('?',144,' '))

Add the regexp_replace to the other fields as you wish.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.