Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use regexp_instr to get the last number in a string

If I used the following expression, the result should be 1.

regexp_instr('500 Oracle Parkway, Redwood Shores, CA','[[:digit:]]')

Is there a way to make this look for the last number in the string? If I were to look for the last number in the above example, it should return 3.

like image 462
Anna Lam Avatar asked Mar 18 '12 03:03

Anna Lam


People also ask

How do you find the last occurrence of a character in a string in Oracle?

Description. The Oracle INSTR function is used to search string for substring and find the location of the substring in the string. If a substring that is equal to substring is found, then the function returns an integer indicating the position of the first character of this substring.

What is REGEXP_INSTR in SQL?

Description. The Oracle/PLSQL REGEXP_INSTR function is an extension of the INSTR function. It returns the location of a regular expression pattern in a string. This function, introduced in Oracle 10g, will allow you to find a substring in a string using regular expression pattern matching.

How do you find the index of a character in a string in Oracle?

The INSTR functions search string for substring . The function returns an integer indicating the position of the character in string that is the first character of this occurrence. INSTR calculates strings using characters as defined by the input character set.

How do you find the first occurrence of a character in a string in Oracle?

The INSTR function returns a numeric value. The first position in the string is 1. If substring is not found in string, then the INSTR function will return 0. If string is NULL, then the INSTR function will return NULL.


2 Answers

If you were using 11g, you could use regexp_count to determine the number of times that a pattern exists in the string and feed that into the regexp_instr

regexp_instr( str,
              '[[:digit:]]',
              1,
              regexp_count( str, '[[:digit:]]')
            )

Since you're on 10g, however, the simplest option is probably to reverse the string and subtract the position that is found from the length of the string

length(str) - regexp_instr(reverse(str),'[[:digit:]]') + 1

Both approaches should work in 11g

SQL> ed
Wrote file afiedt.buf

  1  with x as (
  2    select '500 Oracle Parkway, Redwood Shores, CA' str
  3      from dual
  4  )
  5  select length(str) - regexp_instr(reverse(str),'[[:digit:]]') + 1,
  6         regexp_instr( str,
  7                       '[[:digit:]]',
  8                       1,
  9                       regexp_count( str, '[[:digit:]]')
 10                     )
 11*   from x
SQL> /

LENGTH(STR)-REGEXP_INSTR(REVERSE(STR),'[[:DIGIT:]]')+1
------------------------------------------------------
REGEXP_INSTR(STR,'[[:DIGIT:]]',1,REGEXP_COUNT(STR,'[[:DIGIT:]]'))
-----------------------------------------------------------------
                                                     3
                                                                3
like image 194
Justin Cave Avatar answered Sep 30 '22 14:09

Justin Cave


Another solution with less effort is

SELECT regexp_instr('500 Oracle Parkway, Redwood Shores, CA','[^[:digit:]]*$')-1 
FROM dual;

this can be read as.. find the non-digits at the end of the string. and subtract 1. which will give the position of the last digit in the string..

REGEXP_INSTR('500ORACLEPARKWAY,REDWOODSHORES,CA','[^[:DIGIT:]]*$')-1
--------------------------------------------------------------------
                                                                   3 

which i think is what you want.

(tested on 11g)

like image 22
ShoeLace Avatar answered Sep 30 '22 16:09

ShoeLace