0

I need to extract a text value from data in a VARCHAR2 column. Sample:

EDKES^Visit: ^PRIMARY INSURANCE COMMENTS: ^SECONDARY INSURANCE COMMENTS: ^TERTIARY INSURANCE COMMENTS: ^NO PRIMARY INSURANCE^NONE^NONE^NONE^NONE^NONE^NONE^NONE^NONE^NONE^NO SECONDARY INSURANCE^NONE^NONE^NONE^NONE^NONE^NONE^NONE^NONE^NONE^NO TERTIARY INS*

I need to get the text that proceeds the 6th occurrence of the '^' (excluding the '^'). In this example, the text would be NO PRIMARY INSURANCE.

([\w\s\:\*]+(\^?)) mostly works, but doesn't exclude the '^'.

When I try to use this expression REGEXP_SUBSTR(VARCHAR_COL, '([\w\s\:\*]+(\^?))', 1, 6), I get a single character ('s'), rather than the expected match NO PRIMARY INSURANCE^.

What am I missing?

2 Answers 2

1

This should work pretty well:

REPLACE(REGEXP_SUBSTR(VARCHAR_COL, '[^^]+\^?', 1, 6), '^', '')
Sign up to request clarification or add additional context in comments.

5 Comments

Does '^^' mean all characters but '^'?
@craig Exactly, but only when used as a character class: [^^]. Used on its own ^^ it matches the start of the string (twice).
the expression doesn't work as expected if a position doesn't have a value. For example, applying the REGEX you suggested to ONE^TWO^THREE^^FIVE^SIX^SEVEN would return SEVEN, when I need it to return SIX. Can you recommend a solution?
Interestingly, [^^]+\^?? will eliminate the ^ from the results, so the REPLACE function isn't necessary.
@craig Try '[^^]*\^?'. The + requires at least one character, the * makes it optional. I have no way to test it, but it should work.
1

You might be able to account for blank columns as well. And if the engine only returns
the capture groups, it will trim the delimiter.

([^^]*).?

This of course means that the last column found is always invalid.

1 Comment

Yeah, it looks like its limited to POSIX style. No lookarounds. If you are sure there will be no blank fields (ie ^^^this^that^^) you should be able to use [^^]+ and this trims the delimeter but it won't accept blank fields.

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.