Home » SQL & PL/SQL » SQL & PL/SQL » regexp_substr Ending with Number (Oracle 11g, 11.2.0.2 Linux 64b)
regexp_substr Ending with Number [message #572054] Wed, 05 December 2012 11:12 Go to next message
suj_it
Messages: 3
Registered: December 2012
Junior Member
Is there a way to find out the ending digits in a string?

Examples of input and outputs:
'ABC123' -> 123
'ABC' -> NULL
'123ABC' -> NULL
'123ABC456' -> 456
'123ABC456QP98' -> 98

I have figured a way to do it by doing Reverse and re-reversing using:
reverse(regexp_substr(reverse(p_string), '[ [:digit:]]*'))

But I'm sure there is a way to do it backwards without using reverse function, which I am not able to properly put in the syntax.

Re: regexp_substr Ending with Number [message #572057 is a reply to message #572054] Wed, 05 December 2012 11:24 Go to previous messageGo to next message
BlackSwan
Messages: 22688
Registered: January 2009
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/
Re: regexp_substr Ending with Number [message #572058 is a reply to message #572054] Wed, 05 December 2012 11:35 Go to previous messageGo to next message
Michel Cadot
Messages: 58856
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Welcome to the forum.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version, with 4 decimals.

With any SQL or PL/SQL question, please, Post a working [[Test case]]: create table and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

SQL> with data as (
  2  Select 'ABC123' v from dual union all
  3  select 'ABC' v from dual union all
  4  select '123ABC' v from dual union all
  5  select '123ABC456' v from dual union all
  6  select '123ABC456QP98' v from dual 
  7  )
  8  select v, regexp_substr(v, '\d+$') v 
  9  from data
 10  /
V             V
------------- -------------
ABC123        123
ABC
123ABC
123ABC456     456
123ABC456QP98 98

Regards
Michel
Re: regexp_substr Ending with Number [message #572061 is a reply to message #572054] Wed, 05 December 2012 12:51 Go to previous message
Solomon Yakobson
Messages: 1994
Registered: January 2010
Senior Member
Just for fun without regular expressions:

with data as (
              select 'ABC123' v from dual union all
              select 'ABC' v from dual union all
              select '123ABC' v from dual union all
              select '123ABC456' v from dual union all
              select '123ABC456QP98' v from dual 
             )
select  v,
        substr(v,length('X' || rtrim(v,'0123456789'))) v 
  from  data
/

V             V
------------- ---
ABC123        123
ABC
123ABC
123ABC456     456
123ABC456QP98 98

SQL> 


SY.
Previous Topic: Create View from Dynamic Query (or Function Returning Query)
Next Topic: Object dependencies in PL/SQL code
Goto Forum:
  


Current Time: Thu Aug 21 23:38:34 CDT 2014

Total time taken to generate the page: 0.08529 seconds