I am finding out very quickly that the REGEXP_SUBSTR help online is 100% not resonating with me (Oracle SQL). I believe I have a relatively simple use case, but I'm having a hard time tracking how one REG_SUBSTR argument I'm using is giving me expected results, and why one eludes me. Any help on the use case at hand would be appreciated, as well as a plain language approach to how maybe I could better wrap my head around using, what appears to be a very useful SQL tool.
I have a characteristic value field that has 2 pieces of information I need to extract. Each field will have the following 'A#,P#' - where the # could be 1 digit, all the way to 6 or 7 digits long. I need to extract each of these digits following the 'A', and the 'P'
SO - my first REGEXP_SUBSTR gives me expected results:
SELECT REGEXP_SUBSTR(char_field, '[^,]+',2) a_units
char_field => a_units p_units
A5,P0 5 0
A15,P1 15 1
A6,P12 6 12
A0,P5 0 5
That seems like it does the trick with extracting the entirety of the values following the 'A' in the field. But, the logic doesn't track for me as to why this works - is the '^' only grabbing information to the left of the comma, and after the 2nd character (',2'). Why is the (+) sign there? Could I use the (-) sign to garner anything different? Could someone please explain to me WHY this works, and how I might do the same thing - extract the number(s) after the 'P' in the field? Thanks in advance!
In addition to not understanding why the logic of the a_units REGEXP_SUBSTR formula works for what I need, I'm stuck with how to do the same with p_units. I have tried multiple iterations of changing the order of the [,^] or the number following the comma. Oracle documentation doesn't seem to have examples of this use case available, and the existing documentation throws me for a loop when trying to learn it.
The regex
[^,]+in your example mean matching any non-',' character for length 1+.REGEXP_SUBSTR(char_field, '[^,]+',2) means matching char_filed on regex
[^,]+starting from the2ndcharacter.This won't work for 2nd part of matching your need.
Here is the regex match which meets your need (regex101 verification):
It means matching a list of digit with length 1+
Here is the final query you want: