471,853 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

Help w/ stored proc creation - extract only numeric from alphanumeric

aj
DB2 WSE 8.1 FP 5
Red Hat Linux AS 2.1
(LUW)

I am looking to write a stored procedure
GETNUMBER(p_input VARCHAR(50)) RETURNS Integer
that will return to me only the numeric portion of
alphanumeric p_input, dropping the alpha portion(s).

So,
GETNUMBER('B999') would return 999
GETNUMBER('B888A') would return 888
GETNUMBER('ABCDEFG123') would return 123
GETNUMBER('12345YXEWQ') would return 12345

You get the idea.....

I have this written as a Java UDF, but have had nothing but
headaches w/ v8 FP5 Java VM FMP issues.

Can this be written as a stored procedure?

Any help appreciated.

aj

Nov 12 '05 #1
2 2566
Ian
aj wrote:
DB2 WSE 8.1 FP 5
Red Hat Linux AS 2.1
(LUW)

I am looking to write a stored procedure
GETNUMBER(p_input VARCHAR(50)) RETURNS Integer
that will return to me only the numeric portion of
alphanumeric p_input, dropping the alpha portion(s).

So,
GETNUMBER('B999') would return 999
GETNUMBER('B888A') would return 888
GETNUMBER('ABCDEFG123') would return 123
GETNUMBER('12345YXEWQ') would return 12345


TRANSLATE can help, although it's not so easy without character classes
(since you basically have to specify every character except for 0-9.

INT(TRANSLATE('B999','','AaBbCcDdEeFfGgHhIiJjKkLlM mNnOoPpQqRrSsTtUuVvWwXxYyZz'))
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Nov 12 '05 #2
Why do you want to implement as stored procedure?
It's easy to implement as SQL UDF.
For example:
CREATE FUNCTION GETNUMBER(p_input VARCHAR(50))
RETURNS Integer
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
NO EXTERNAL ACTION
RETURN
CAST(REPLACE(TRANSLATE(p_input, '', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
' ', '') AS INTEGER)
!
Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by shan | last post: by
45 posts views Thread by John | last post: by
1 post views Thread by Rahul | last post: by
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.