Home » SQL & PL/SQL » SQL & PL/SQL » groups in regexp_instr (Oracle 10g)
groups in regexp_instr [message #643406] Wed, 07 October 2015 02:50 Go to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
I want to extract the numbers part from the string of second column bs_Desc in a separate columns, i am using the following method to achieve this ,The problem is 2 reference position is not fixed.Only thing common is 1 number occurrence and 2nd number occurrence. i want to know whether it can be done using reference and if there is a solution without regular expressions.


CREATE TABLE BOLTS ( BS_CODE VARCHAR2(12),BS_DESC VARCHAR2(30));

INSERT INTO BOLTS(BS_CODE,BS_DESC) VALUES ('1','BOLTS_1NUTS_1WASHERS');

INSERT INTO BOLTS(BS_CODE,BS_DESC) VALUES ('2','BOLTS_2NUTS_1WASHERS');

INSERT INTO BOLTS(BS_CODE,BS_DESC) VALUES ('3','BOLTS_1NUTS_2WASHERS');


SELECT bs_desc,
       SUBSTR (bs_desc,
               REGEXP_INSTR (bs_desc, '(\d)', 1, 1, 0),
               1
              ) first_reference,
       SUBSTR (bs_desc,
               REGEXP_INSTR (bs_desc, '(\d)', 8, 1, 0),
               1
              ) second_number
  FROM bolts;


FIRST_REFERENCE,SECOND_NUMBER
1,1
2,1
1,2





Re: groups in regexp_instr [message #643411 is a reply to message #643406] Wed, 07 October 2015 04:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> select bs_desc, regexp_substr(bs_desc,'(\d)') first,
  2         regexp_substr(bs_desc,'(\d)',1,2) second
  3  from BOLTS
  4  /
BS_DESC                        FIRST                          SECOND
------------------------------ ------------------------------ ------------------------------
BOLTS_1NUTS_1WASHERS           1                              1
BOLTS_2NUTS_1WASHERS           2                              1
BOLTS_1NUTS_2WASHERS           1                              2

Re: groups in regexp_instr [message #643412 is a reply to message #643411] Wed, 07 October 2015 04:47 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
thanks Michael
Re: groups in regexp_instr [message #643413 is a reply to message #643412] Wed, 07 October 2015 04:50 Go to previous messageGo to next message
bugfox
Messages: 18
Registered: October 2010
Junior Member
without regexp:
select substr(bs_desc, instr(translate(bs_desc, '1234567890', '##########'), '#'), 1) first_reference,
substr(bs_desc, instr(translate(bs_desc, '1234567890', '##########'), '#', -1), 1) second_number
from bolts
Re: groups in regexp_instr [message #643426 is a reply to message #643411] Wed, 07 October 2015 07:36 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
You probably meant:


select bs_desc, regexp_substr(bs_desc,'\d+') first,
regexp_substr(bs_desc,'\d+',1,2) second
from BOLTS
/


SY.
Re: groups in regexp_instr [message #643428 is a reply to message #643426] Wed, 07 October 2015 07:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

No, it is useful only if the number can contain several digits.
I let this for a future extension of the original question...
(Note I used + when I first tested my query and remove it when I posted. Smile )

Re: groups in regexp_instr [message #643593 is a reply to message #643428] Tue, 13 October 2015 04:12 Go to previous message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
thanks bugfox ,solomon and michael.
Previous Topic: How to get weeknum for the given date
Next Topic: HOW TO STORE ROWID DATA BY USING ROWID DATA TYPE?
Goto Forum:
  


Current Time: Thu Apr 25 14:02:46 CDT 2024