Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DB2 stored procedure for removing the special character from db

Posted on 2008-06-11
14
Medium Priority
?
3,601 Views
Last Modified: 2012-08-14
hi all,
 i want to have one stored procedure to remove special characters from dbase and print the report
 and after that it will concatenate the other string and update the db.
i am using db2.
ex-- ASDBH&WE -----> ASDBHWE
 
        ASDB_EW ----->ASDBEW

       ASEDE%DV---->ASEDEDV
       
0
Comment
Question by:mukeshkt
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 21758492
what do you mean - remove from db?
do you need to remove from a specific table or from all tables ?
basically this can be done using a single sql statement

update table_name
set column_name = case when posstr(column_name, '&') > 0 then replace(columns_name, '&', '')
   when posstr(column_name, '_') > 0 then replace(columns_name, '_', '')
   when posstr(column_name, '%') > 0 then replace(columns_name, '%', '')
 end
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 672 total points
ID: 21759107
Hi mukeshkt,

Momi's on the right track and if the value will have no more than 1 type of special character (only '&' or only '%', etc.) use his solution.  Order the CASE conditions according to likelihood.

If the value could contain several different special characters, you'll probably want to catch all of them with one statement.  Nesting replace() functions will work fine.

UPDATE mytable
SET somecolumn =
  replace (replace, (replace (somecolumn, '&', ''), '_', ''), '%', '')

Variations on this will report on the columns to be affected:

SELECT id, somecolumn, replace (replace, (replace (somecolumn, '&', ''), '_', ''), '%', '')
FROM mytable
WHERE somecolumn <> replace (replace, (replace (somecolumn, '&', ''), '_', ''), '%', '')


Good Luck,
Kent
0
 
LVL 18

Expert Comment

by:Dave Ford
ID: 21760224

Here's the way I usually accomplish that kind of thing:

upda
set text = translate(text,' ','&_%', ' ');

I find that much simpler than multiple levels of REPLACE functions.

HTH,
DaveSlash
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 21760302

Hi, Dave!

Great idea, except that I think the poster wants the character(s) removed from the name, not replaced with a blank.


Kent


0
 
LVL 18

Assisted Solution

by:Dave Ford
Dave Ford earned 664 total points
ID: 21760350

Simple enough ...

update ...
set text = translate(text,'','&_%', '');
0
 
LVL 37

Assisted Solution

by:momi_sabag
momi_sabag earned 664 total points
ID: 21760417
translate can not remove a character, it can only replace characters
you could use your translate to change the bad characters to blanks and then use replace to remove them
but this will require more work so i used the replcae directly
0
 
LVL 18

Expert Comment

by:Dave Ford
ID: 21761406

Excellent point, Momi.

How about this:
replace(translate(avarchar25,'~','abc', '~'),'~', '')

As long as you can ensure that there are no tildes in the string, then that should work.

-- DaveSlash
0
 
LVL 18

Expert Comment

by:Dave Ford
ID: 21761428

or more accurately:

replace(translate(avarchar25,'~','&_%', '~'),'~', '')
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 21761635
Hi Dave.

Just change all of the questionable characters to the same questionable character with translate, then delete it with a call to replace.  No hunting for an unused character is necessary.

  replace(translate(avarchar25,'&','_%', '&'),'&', '')


Speaking of questionable characters...

Well, never mind.  :)


Kent
0
 
LVL 18

Expert Comment

by:Dave Ford
ID: 21761663

> Speaking of questionable characters

Hey, I resemble that remark!  :-)  :-)
0
 

Author Comment

by:mukeshkt
ID: 22117380
please close this question
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22118020
was your problem solved?
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
Loops Section Overview
Screencast - Getting to Know the Pipeline

876 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