If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Extract number from string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-06-12, 12:35
oteixeira oteixeira is offline
Registered User
 
Join Date: Jun 2007
Posts: 8
Extract number from string

Hello to all.

I'm new to sqlplus and trying to do the following:
I have some strings that may be like 'ABC*9497' or 'ABCD*9497'. How can i extract just the numeric part (9497)? I've been trying to use TO_NUMBER function in a seriuos of formats but could not get it.
Can someone please give some help? The number of characters up to the asterisk can vary.
Thanks in advance for any kind help.
Octavio
Reply With Quote
  #2 (permalink)  
Old 01-06-12, 12:43
anacedent anacedent is offline
Registered User
 
Join Date: Aug 2003
Location: Where the Surf Meets the Turf @Del Mar, CA
Posts: 6,416
select substr('ABC*9497',(instr('ABC*9497','*')+1)) from dual;

SUBS
----
9497
__________________
You can lead some folks to knowledge, but you can not make them think.
The average person thinks he's above average!
For most folks, they don't know, what they don't know.
Reply With Quote
  #3 (permalink)  
Old 01-06-12, 12:48
oteixeira oteixeira is offline
Registered User
 
Join Date: Jun 2007
Posts: 8
Anacedent, thanks a million.
Octavio
Reply With Quote
  #4 (permalink)  
Old 01-07-12, 04:54
Littlefoot Littlefoot is offline
Lost Boy
 
Join Date: Jan 2004
Location: Croatia, Europe
Posts: 3,629
Or, using regular expressions:
Code:
SQL> with test as
  2    (select 'ABC*9497' col from dual union
  3     select 'ABCD*234' from dual
  4    )
  5  select col, regexp_substr(col, '\d+$') numbers_only
  6  from test;

COL      NUMBERS_ONLY
-------- --------------------------------
ABC*9497 9497
ABCD*234 234

SQL>
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On