No. of occurences in oracle

Posted on 2011-05-02
Last Modified: 2012-05-11

I have a string 'aabbbccc''

Now, I wanted to find out how many 'a' are there in this string.. Can someone guide me?

Quick response is appreciated!
Question by:d27m11y
    LVL 76

    Accepted Solution

    The quick and dirty something like:
    select length('aabbbccc')-length(replace('aabbbccc','a')) from dual;

    If we knew a little more about the actual requirements we might be able to clean it up some.
    LVL 40

    Assisted Solution

    Another using regex, trims all non a characters, then computes length of what is left.

     select length(regexp_replace('aabbbaccc', '[^a]', '')) from dual;

    And a case-insensitive approach:

     select length(regexp_replace('aabbbAccc', '[^Aa]', '')) from dual;
    LVL 23

    Assisted Solution

    - if you are on Oracle 11g, you can use REGEXP_COUNT as follows:
    SELECT 'aabbbccc', REGEXP_COUNT('aabbbccc','a') counter FROM dual
    SELECT columnA, REGEXP_COUNT(columnA,'a') counter FROM tblname

    - more REGEXP_COUNT option here:
    LVL 31

    Expert Comment

    Since the third parameter of regexp_replace defaults to null, you can further simplify mrjoltcola's expression to
    select length(regexp_replace('aabbbaccc', '[^a]')) from dual;

    Author Closing Comment

    Easy to follow!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Oracle 10g - Count Rows excluding 0's 4 47
    Update a field datetime 3 30
    import data 5 47
    Remove Hyphens in Oracle SQL 5 35
    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…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    730 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

    16 Experts available now in Live!

    Get 1:1 Help Now