Solved

regexp_count not working

Posted on 2013-01-23
9
2,445 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
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: 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 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

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.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

777 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