Solved

regexp_count not working

Posted on 2013-01-23
9
2,558 Views
Last Modified: 2013-01-24
I created a view called v with column called string;

--this works
select string from v;

returns:
www.oracle-developer.net

--problem
SELECT REGEXP_COUNT(string, 'e') AS "e Count"
    FROM   v;

why do I get error:
ORA-00904: "REGEXP_COUNT": invalid identifier
0
Comment
Question by:claghorn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38812022
What is your database version?
I believe regex_count is new in 11g.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38812032
The 10gR2 docs only show:
REGEXP_INSTR
REGEXP_REPLACE
REGEXP_SUBSTR


http://docs.oracle.com/cd/B19306_01/server.102/b14200/toc.htm
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 127 total points
ID: 38812042
The 11gR1 docs have it:
REGEXP_COUNT
REGEXP_INSTR
REGEXP_REPLACE
REGEXP_SUBSTR

http://docs.oracle.com/cd/B28359_01/server.111/b28286/toc.htm
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 74

Expert Comment

by:sdstuber
ID: 38812079
slightwv is correct, regexp_count is only available in 11g and above.

you can however, write your own....

Here's a simplified version, it simply looks for expressions within a string but I didn't include the position and match param  options



CREATE OR REPLACE FUNCTION myregexp_count(p_string IN VARCHAR2, p_expression IN VARCHAR2)
    RETURN INTEGER
IS
    v_cnt      INTEGER := 0;
    v_string   VARCHAR2(32767) := p_string;
BEGIN
    WHILE REGEXP_INSTR(v_string, p_expression) > 0
    LOOP
        v_string  := REGEXP_REPLACE(v_string, p_expression, null,1, 1);
        v_cnt     := v_cnt + 1;
    END LOOP;

    RETURN v_cnt;
END;
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 38812088
this is a more complete version that supports the position and match param options



CREATE OR REPLACE FUNCTION myregexp_count(p_string        IN VARCHAR2,
                                          p_expression    IN VARCHAR2,
                                          p_position      IN INTEGER DEFAULT 1,
                                          p_match_param   IN VARCHAR2 DEFAULT NULL
                                         )
    RETURN INTEGER
IS
    v_cnt      INTEGER := 0;
    v_string   VARCHAR2(32767) := SUBSTR(p_string, p_position);
BEGIN
    WHILE REGEXP_INSTR(v_string,
                       p_expression,
                       1,
                       1,
                       0,
                       p_match_param
                      ) > 0
    LOOP
        v_string      :=
            REGEXP_REPLACE(v_string,
                           p_expression,
                           NULL,
                           1,
                           1,
                           p_match_param
                          );
        v_cnt  := v_cnt + 1;
    END LOOP;

    RETURN v_cnt;
END;
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 128 total points
ID: 38812148
more efficient version of above - no need to do the replace, just increment the occurence counter on the instr



CREATE OR REPLACE FUNCTION myregexp_count(p_string        IN VARCHAR2,
                                           p_expression    IN VARCHAR2,
                                           p_position      IN INTEGER DEFAULT 1,
                                           p_match_param   IN VARCHAR2 DEFAULT NULL
                                          )
    RETURN INTEGER
IS
    v_cnt   INTEGER := 0;
BEGIN
    WHILE REGEXP_INSTR(p_string,
                       p_expression,
                       p_position,
                       v_cnt + 1,
                       0,
                       p_match_param
                      ) > 0
    LOOP
        v_cnt  := v_cnt + 1;
    END LOOP;

    RETURN v_cnt;
END;
0
 
LVL 32

Expert Comment

by:awking00
ID: 38815109
claghorn, you're showing Oracle 11g as a tag. Are you really using 11g? If so, there's no reason that regexp_count shouldn't work. How are you submitting the query?
0
 
LVL 32

Expert Comment

by:awking00
ID: 38815119
Works for me -
SQL> create view v(string) as select 'www.oracle-developer.net' from dual;

View created.

SQL> select * from v;

STRING
------------------------
www.oracle-developer.net

SQL> select regexp_count(string,'e') as "e Count" from v;

   e Count
----------
         5
0
 

Author Closing Comment

by:claghorn
ID: 38816117
I thought I was on 11g but it was 10g. Thanks for the workaround as an extra.
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question