Skip to content
Advertisement

Extract a desired character from an alphanumeric column using concise REGEX in Oracle SQL

I am going through a “clean-up” process of a character column that holds employee rankings that should have a single character: 0-5 or M or U but instead it has up to 3 characters that are either numeric, alpha or alphanumeric.

I spent the better part of two days researching online (Regex, Stack overflow and Oracle resources) and testing and trying various options and the below “RESULT” (refer to code) is what I came up with. It does the job but I can’t help but think that there is a more concise way of doing this. For example, at one point I thought I was close to accomplishing the task with a single instance of REGEXP_SUBSTR which used “|” (refer to PREV_TRY in code below). But then I couldn’t figure out how to take this result and extract from it the first character for cases [1-4]{1}[ABCUL]{1} or the last character for all other cases.

Here is a reproducible example along with the solution I have so far:

I summarize what I need to accomplish with these rules:

  • if its a single character then return any character that matches [0-5MU].
  • if its a single digit followed by a single alpha character then return the digit [1-4]{1}[ABCUL]{1}. E.g., ‘2A’ returns ‘2’
  • if its RG[1-4] then return the digit. E.g., ‘RG3’ returns ‘3’
  • if its 0[1-4] then return the second digit. E.g., ’03’ returns ‘3’
  • all else return 0

Advertisement

Answer

You can use:

Which outputs:

EX REPLACEMENT
M M
U U
1A 1
1 1
2 2
3 3
4 4
5 5
6 0
7 0
0 0
113 0
3 3
99 0
RG1 1
NA 0
<null> 0

db<>fiddle here

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement