0

In an Oracle database I have a string such as 12.13 this is a test. Using regexp_substr I'm trying to extract just 13. That is the number after the dot and before the space. I've tried various combinations and (string, '\.[0-9]{1,}') comes closest but I still get .13. Is there any way to just return 13?

The data also includes items like 1.1. At most the numbers will go 99.99.

All possible number formats are: #.#, #.##,##.#,##.##

4
  • Thanks RedRaven. Do all the numbers have a decimal . or only some? Commented Apr 1, 2019 at 22:14
  • @alexgibbs: see edit for possible formats. All numbers have a decimal. I want everything after the decimal, but not the decimal. Commented Apr 1, 2019 at 22:21
  • @RedRaven from a pure Regex perspective you need a positive lookbehind, however, it doesn't appear that this functionality is supported in Oracle as discussed here Commented Apr 1, 2019 at 22:27
  • Will there always be a space after the digits you need, or could the number (like 12.13 in your example) be at the end of the string, so there would be no space after 13? Commented Apr 2, 2019 at 5:01

3 Answers 3

2

Given the input strings will always have leading numbers and a dot, one can extract the fractional numbers with regexp_substr. regexp_replace could also be used.

Here are a few examples for regexp_substr. These work by including a capturing group targeting the desired data, and a subexpression argument (the last argument in regexp_substr):

SELECT REGEXP_SUBSTR('12.13 this is a test','^[0-9]{1,}[.]{1}([0-9]{1,})',1,1,NULL,1) AS FRACTIONAL FROM DUAL;

Result:

FRACTIONAL
13

1 row selected.

SELECT REGEXP_SUBSTR('1.1 this is a test','^[0-9]{1,}[.]{1}([0-9]{1,})',1,1,NULL,1) AS FRACTIONAL FROM DUAL;

Result:

FRACTIONAL
1

1 row selected.

SELECT REGEXP_SUBSTR('99.99 this is a test','^[0-9]{1,}[.]{1}([0-9]{1,})',1,1,NULL,1) AS FRACTIONAL FROM DUAL;

Result:

FRACTIONAL
99

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

Comments

1

I like regexp_replace better than regexp_substr for such tasks.

regexp_replace(str, '^.*?\.([[:digit:]]+).*$', '\1');

i.e. look for the wohle string from the start, with some characters, a point, some digits, some characters until the end, but remember only the digits and return those instead of the string.

But I guess that's just a matter of personal preference.

Comments

0
select regexp_substr('12.13 this is a test', '\d+\.(\S+)', 1, 1, null, 1) rs
from dual;

RS
--
13

2 Comments

Add some explanation to improve quality of your answer

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.