By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,514 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,514 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.