How to Write & call a user defined function in UDB stored procedure?

Hi Experts,
I need to write a function which should take a character parameter & return a character paramenter, this function will be in UDB stored procedure & also have call from same SP. Can anybody help how to write a function UDB SP & make call to it in same SP. Appreciate any sample code for same.

Regards
Ams
Amit_PekamwarAsked:
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.

momi_sabagCommented:
what should the function do?
0
Amit_PekamwarAuthor Commented:
It should -
accept a character string
Remove non numeric values from that string &
Return the string with only numeric values in it.
0
momi_sabagCommented:
udb already has a built in function for that, sort of, you can do it without creating your own function like this:

select
  replace(translate(your_string, '', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'), ' ', '')



read about translate:
http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0000862.htm

and replace:
http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0000843.htm
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Amit_PekamwarAuthor Commented:
Thanks Momi,
we tried translate & it is not working as per our requirement. Hence we are using our own logic to convert same. That logic we need to execute multiple times in our SP. So thinking if we can incorporate a function in SP & call that function multiple times.
0
momi_sabagCommented:
here is an example of a function
CREATE FUNCTION REVERSE(INSTR VARCHAR(4000))
     RETURNS VARCHAR(4000)
     DETERMINISTIC NO EXTERNAL ACTION CONTAINS SQL
     BEGIN ATOMIC
     DECLARE REVSTR, RESTSTR VARCHAR(4000) DEFAULT '';
     DECLARE LEN INT;
     IF INSTR IS NULL THEN
     RETURN NULL;
     END IF;
     SET (RESTSTR, LEN) = (INSTR, LENGTH(INSTR));
     WHILE LEN > 0 DO
     SET (REVSTR, RESTSTR, LEN)
       = (SUBSTR(RESTSTR, 1, 1) CONCAT REVSTR,
       SUBSTR(RESTSTR, 2, LEN - 1),
       LEN - 1);
     END WHILE;
     RETURN REVSTR;
   END

you can find it here
http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0003493.htm

calling a function you create is just like calling a built-in db2 function so no special needs there
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
Amit_PekamwarAuthor Commented:
Thanks Momi. Example provided is working fine & I have created similar function for my functionality. Thanks a lot again.
0
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
DB2

From novice to tech pro — start learning today.