0

I have this inputs:

John/Bean/4000-M100
John/4000-M100
John/4000

How can I get just the 4000 but note that the 4000 there will be change from time to time it can be 3000 or 2000 how can I treat that using regex pattern?

Here's my output so far, it statisfies John/400-M100 and John/4000 but the double slash doesnt suffice the match requirements in the regex I have:

REGEXP_REPLACE(REGEXP_SUBSTR(a.demand,'/(.*)-|/(.*)',1,1),'-|/','')
2
  • Which SQL variety are you using? Commented Mar 6, 2019 at 0:15
  • Oracle SQL 12c @Nick Commented Mar 6, 2019 at 0:22

3 Answers 3

1

You can use this query to get the results you want:

select regexp_replace(data, '^.*/(\d{4})[^/]*$', '\1')
from test

The regex looks for a set of 4 digits following a / and then not followed by another / before the end of the line and replaces the entire content of the string with those 4 digits.

Demo on dbfiddle

Sign up to request clarification or add additional context in comments.

3 Comments

pretty much sure right now the code is working especially on Nick, just had to minor tweaks and it work! :) Thank you
Hi @Nick! how about just a hunch if ever we have more than 4 digits like 5 digits or 6 digits? Sample below: SCP_FCSTPLAN_DFCann_SCJ_APP_YES_o7120/14161-M_02
@遊星不動 you can just change the {4} to match appropriately e.g. {5} or if you could have 4 or 5 or 6 you can make it {4,6}
0

This would also work, unless you need any digit followed by three zeros. See it in action here, for as long as it lives, http://sqlfiddle.com/#!4/23656/5

create table test_table 
( data varchar2(200))
insert into test_table values('John/Bean/4000-M100')
insert into test_table values('John/4000-M100')
insert into test_table values('John/4000')

select a.*,
       replace(REGEXP_SUBSTR(a.data,'/\d{4}'), '/', '')
from   test_table a

2 Comments

It seems okay but there are some instance like: Sal_AS_180763852/4200009751_S5_154552/4023 its getting the 4 digits for the first slash "/4200" instead of "/4023"
pretty much sure right now the code is working especially on Nick, just had to minor tweaks and it work! :) Thank you
0

The following will match any multiple of 1000 less than 10000 when its preceded by a slash:

\/[1-9]0{3}

To match any four-digit number preceded by a slash, not followed by another digit, such as 4031 in—

Sal_AS_180763852/4200009751_S5_154552/4031

—try:

\/\d{3}(?:(?:\d[^\d])|(?:\d$))

https://regex101.com/r/Am34WO/1

5 Comments

how about this one? how do you treat this @sean to match only 4031? Sal_AS_180763852/4200009751_S5_154552/4031
it returns null on sql query:
SELECT REGEXP_REPLACE(REGEXP_SUBSTR(a.demand,'\/(\d{3}(?:(?:\d[^\d])|(?:\d$)))',1,1),'-|/','') as test
pretty much sure right now the code is working especially on Nick, just had to minor tweaks and it work! :) Thank you
Hi @Nick! how about just a hunch if ever we have more than 4 digits like 5 digits or 6 digits? Sample below: SCP_FCSTPLAN_DFCann_SCJ_APP_YES_o7120/14161-M_02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.