Solved

DB2 stored procedure for removing the special character from db

Posted on 2008-06-11
14
3,224 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
14 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 45

Accepted Solution

by:
Kdo earned 168 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:daveslash
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 45

Expert Comment

by:Kdo
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:daveslash
daveslash earned 166 total points
ID: 21760350

Simple enough ...

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

Assisted Solution

by:momi_sabag
momi_sabag earned 166 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:daveslash
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:daveslash
ID: 21761428

or more accurately:

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

Expert Comment

by:Kdo
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:daveslash
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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 (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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