467,926 Members | 1,376 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

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
  • viewed: 2439
Share:
2 Replies
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
By using this site, you agree to our Privacy Policy and Terms of Use.