How can Oracle extract numbers from a string?
Regular expressions can be used to extract numbers from a string. The specific steps are as follows:
- Utilize the REGEXP_SUBSTR function to extract the numerical portion of a string.
- Create a regular expression pattern to match numbers.
- Pass the string and regular expression as parameters to the REGEXP_SUBSTR function, and specify the position of the desired result (default is 1).
- Return the extracted numerical part as the result.
Here is an example code:
SELECT REGEXP_SUBSTR('abc123def456', '[[:digit:]]+') AS result FROM dual;
Executing the above code will return a row of data with the result “123”.