Solved

regexp_count not working

Posted on 2013-01-23
9
2,393 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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 76

Expert Comment

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

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 76

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
 
LVL 73

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 73

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 73

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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…

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now