Solved

regexp_count not working

Posted on 2013-01-23
9
2,625 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

636 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