2

I already found out I need to use substr/instr or regex but with reading the documentation about those, I cant get it done... I am here on Oracle 11.2.

So here is what I have. A list of Strings like:

743H5-34L-56
123HD34-7L
12HSS-34R
23Z67-4R-C23

What I need is the number (length 1 or 2) after the first '-' until there comes a 'L' or 'R'.

Has anybody some advice?

2 Answers 2

4
regexp_replace(string, '^.*?-(\d+)[LR].*$', '\1')

fiddle

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

1 Comment

That did the trick, thank you! Very confusing syntax when one is not that deep into that... at least when creating a statement. Understanding is ok with the doku beside me =)
4

Another version (without fancy lookarounds :-) :

with v_data as (
  select '743H5-34L-56' val from dual
  union all
  select '123HD34-7L' val from dual
  union all 
  select '12HSS-34R' val from dual
  union all
  select '23Z67-4R-C23' val from dual
)
select 
  val, 
  regexp_replace(val, '^[^-]+-(\d+)[LR].*', '\1') 
from v_data

It matches

  • the beginning of the string "^"
  • one or more characters that are not a '-' "[^-]+"
  • followed by a '-' "-"
  • followed by one ore more digits (capturing them in a group) "(\d+)"
  • followed by 'L' or 'R' "[LR]"
  • followed by zero or more arbitrary characters ".*"

Comments

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.