Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm trying to extract 456 from the string :123:456: as follows:

select regexp_substr(':123:456:', ':(\d+):', 1, 2, 'i', 1) from dual

However, this query returns null. What am I doing wrong?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

This works fine:

select regexp_substr(':123:456:', '(\d+):', 1, 2, 'i', 1) from dual;

I think yours fails because the opening and closing colons won't get matched by both occurrences (because the first match is greedy).

share|improve this answer
add comment
select regexp_substr(':123:456:','[^:]+',2,2) from dual
share|improve this answer
1  
If this is substantially different from the accepted answer posted by Phil 10 months ago, please add some context explaining why. –  Aaron Bertrand Jul 9 '13 at 13:58
    
This will match on :123:abc: which doesn't sound desirable, but fail to match on ::456: which might be. –  Mat Jul 9 '13 at 17:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.