Solved

regexp_count not working

Posted on 2013-01-23
9
2,360 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)
Comment Utility
What is your database version?
I believe regex_count is new in 11g.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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
Comment Utility
I thought I was on 11g but it was 10g. Thanks for the workaround as an extra.
0

Featured Post

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.

Join & Write a Comment

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…
Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

763 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

15 Experts available now in Live!

Get 1:1 Help Now