1

I want to extract a specific part of column values. The target column and its values look like

TEMP_COL
---------------
DESCOL 10MG
TEGRAL 200MG 50S
COLOSPAS 135MG 30S

The resultant column should look like

RESULT_COL
---------------
10MG
200MG
135MG
4
  • You need to be clearer about the requirements. What are the rules / algorithm? For example, it could be 1. Every 2nd work; 2. The first word starting with a number; 3. Any work with MG as the suffix... etc Commented Mar 8, 2017 at 5:37
  • i didn't think of the algorithm at the time of asking the question. but i believe the required data can be extracted by reversing the column values then find like 'MG%' until the space occurs. although I couldn't figure out how to put the algorithm in actual SQL format. Commented Mar 8, 2017 at 6:50
  • Is it always a number followed by MG that you want to extract? Commented Mar 8, 2017 at 6:53
  • yes. its always a number ending at MG Commented Mar 8, 2017 at 6:54

3 Answers 3

3

This can be done using a regular expression:

SELECT regexp_substr(TEMP_COL, '[0-9]+MG')
FROM the_table;

Note that this is case sensitive and it always returns the first match.

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

1 Comment

This also assumes that the middle term uses milligrams as the unit of measure, which may not always be true.
2

I would probably approach this using REGEXP_SUBSTR() rather than base functions, because the structure of the prescription text varies from record to record.

SELECT TRIM(REGEXP_SUBSTR(TEMP_COL, '(\s)(\S*)', 1, 1))
FROM yourTable

The pattern (\s)(\S*) will match a single space followed by any number of non-space characters. This should match the second term in all cases. We use TRIM() to remove a leading space which is matched and returned.

Comments

0

how do you know what is the part you want to extract? how do you know where it begins and where it ends? using the white-spaces?

if so, you can use substr for cutting the data and instr for finding the white-spaces.

example:

select substr(tempcol, -- string
              instr(tempcol, ' ', 1), -- location of first white-space 
              instr(tempcol, ' ', 1, 2) - instr(tempcol, ' ', 1))  -- length until next space
from dual

another solution is using regexp_substr (but it might be harder on performance if you have a lot of rows):

SELECT REGEXP_SUBSTR (tempcol, '(\S*)(\s*)', 1, 2)
FROM dual;

edit: fixed the regular expression to include expressions that don't have space after the parsed text. sorry about that.. ;)

2 Comments

Your REGEXP_SUBSTR() has a problem. Do you see what it is?
@TimBiegeleisen, yup. fixed.

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.