Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DB2 stored procedure for removing the special character from db

Posted on 2008-06-11
14
Medium Priority
?
3,532 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
[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
  • 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 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 
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

URL rewriting in AWS CloudFront

A quick how-to guide to implement with a Lambda function!

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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

721 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