0

In my location field I have :

"Location:
SOME PLACE

Additional Details:
DEFECT
"

Somehow I need this to take out the "SOME PLACE". I have tried to understand how the regexp_substr() function works, but it is very different from my C# background and I keep getting weird results like nulls.

Normally I would have expected to do something like :

regexp_substr(LocationField, 'Location:(.*)\n') as "NewLocation"

And for it to cut out the part in parenthesis... but I was wrong. Any ideas?

SOLUTION :

regexp_replace(FIELD, 'Location:'||CHR(13)||CHR(10)||'(.*).*$'||CHR(13)||CHR(10)||CHR(13)||CHR(10)||'Additional Details:.*$', '\1', 1, 1, 'n' ) as "Location"

3 Answers 3

1

you can do it like this:

regexp_replace(LocationField, 
               'location:'||chr(10)||'([^'||chr(10)||']*).*$', '\1', 
                1, 1, 'n' ) 

ie the regex string

location:\n([^\n]*).*$

but as oracle will not work with \n we splice in chr(10) in its place.

the n mode means that . will match the newline character (so .*$ gets rid of everything after the newline following the SOME TEXT.

if location can have mulitple lines of text followed by Addition Details, you could use:

regexp_replace(LocationField, 
               'location:'||chr(10)||'(.*)'||chr(10)||'Additional.*$',  
               '\1', 1, 1, 'n' ) as "NewLocation"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer it helped me most in reaching the solution.
0

Use REGEXP_REPLACE instead:

select REGEXP_REPLACE( target, LocationField, 'Location:(.*)\n', '' ) as "NewLocation"
from your_table;

Should get you going in the right direction.

1 Comment

Hmm... you seem to have 4 parameters, and the link has 3? I tried with 3 omitting the target, because the LocationField is the target, and it returned to me the whole field...
0

You need to run the regex in multi-line mode, which means populating the full signature of the REGEXP_SUBSTR() call:

select regexp_substr( locationField, '^([A-Z ]+)$', 1, 1, 'm')
from your_table
/

The 'm' is a match parameter specifying multi-line mode. This means ^ and $ in the matching pattern are treated as newline boundaries rather than the start and end of the string. Find out more.

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.