Counting the character in a string

How to find the number of repeated characters in a string
example- 'RAJA' In this string how many 'A' are there

A beautiful query posted by a guru in this forum long time ago.
SQL> set line 1000
SQL> l
  1     SELECT ename
  2          , Letter_repetition
  3          , COUNT(*)repetition_count
  4     FROM ( SELECT ename
  5                   , Letter_repetition
  6                FROM emp
  7               MODEL
  8                     RETURN UPDATED ROWS
  9                     PARTITION BY (ename)
10                     DIMENSION BY (0 i)
11                     MEASURES (ename Letter_repetition)
12                     ( Letter_repetition[FOR i FROM 1 TO
13                                         length(Letter_repetition[0])
14                                         INCREMENT 1] = SUBSTR(Letter_repetition[0],CV(i),1)
15                     )
16             )--End of FROM clause
17      GROUP BY ename
18          , Letter_repetition
19*    HAVING COUNT(*) > 1
SQL> /
ENAME                               LETTER_REPETITION                   REPETITION_COUNT
SCOTT                               T                                                  2
TURNER                              R                                                  2
ALLEN                               L                                                  2
MILLER                              L                                                  2
ADAMS                               A                                                  2
SQL>

Similar Messages

  • How to escape the character in a string?

    I have the following code that is causing an error. I'm
    attempting to use < (less than) character in a string that is
    part of a dynamic query:
    <cfif url.age eq 1>
    <cfset askAge = " < 15">
    <cfelseif url.age eq 10>
    <cfset askAge = " Between 56 and 60">
    <cfelseif url.age eq 11>
    <cfset askAge = " > 60">
    </cfif>
    The error occurs as a result of the line <cfset askAge = "
    < 15">. The > 60 works fine. What I need is to build the
    SQL statement SELECT * FROM myTable WHERE age < 15
    ColdFusion sets the string properly but when I try to run the
    query I guess it thinks I'm trying to open a tag. The exact error
    is:
    The content beginning "< " is not legal markup. Perhaps
    the " " (&#20;) character should be a letter.
    The content beginning "< " is not legal markup. Perhaps
    the " " () character should be a letter.
    I can't use the ASCII code for this character as the database
    will obviously crap out.
    Any help would be greatly appreciated and many thanks in
    advance.
    Dave

    Dave,
    What if you passed off the output of the lt/gt symbols to the
    query itself?
    Something like,
    <cfif url.age eq 1>
    <cfset compareOperator = "less"/>
    <cfset askAge = "15">
    <cfelseif url.age eq 10>
    <cfset compareOperator = "between"/>
    <cfset askAge = " 56 and 60">
    <cfelseif url.age eq 11>
    <cfset compareOperator = "greater"/>
    <cfset askAge = "60">
    </cfif>
    Then in the query:
    <cfquery name="qTest" datasource="dsn">
    SELECT * FROM myTable
    WHERE age
    <cfswitch expression="#compareOperator#">
    <cfcase value="less">
    <
    </cfcase>
    <cfcase value="greater">
    >
    </cfcase>
    <cfcase value="between">
    BETWEEN
    </cfcase>
    <cfdefaultcase>
    =
    </cfdefaultcase>
    </cfswitch>
    #askAge#
    </cfquery>
    Probably a better way, at least a more efficient way, but
    just a thought...cfswitch processed pretty quickly and this would
    remove any issues with trying to use <> symbols.
    Cheers,
    Craig

  • Count of character in a string

    Hi,
    Here is a string.
    Str = 'test,123,1-Jan-2008',sql,oracle,test,date
    Can we count the number of commas(,) in the above string without passing it to any loop? I want to use replace/translate and length for this purpose.
    Regards,
    Ritesh

    SQL> var a varchar2(500)
    SQL> exec :a := '''test,123,1-Jan-2008'',sql,oracle,test,date'
    PL/SQL procedure successfully completed.
    SQL> print a
    A
    'test,123,1-Jan-2008',sql,oracle,test,date
    SQL> select length(:a)-length(replace(:a,',')) comma_count
      2    from dual
      3  /
    COMMA_COUNT
              6

  • Counting particular character in a string

    Hi all
    how can we count the number of characters in a perticular string in simple SQL statement
    example
    how many commas are there in this string 'a,b,s,d,e,f,g,h'

    select length('a,b,c,d,e')-length(replace('a,b,c,d,e',','))
    from dual

  • Replacing the \ character in a string

    I'm having trouble replacing a backslash in a string. I use .indexOf() to find the character, but I have to use an escape character and write .index("\\"), but it doesn't work - it returns -1.
    Also, replace() doesn't work - gives me an java.util.regex.PatternSyntaxException. Any ideas?
    thx

    The escape is for the compiler, I think, and then you need to escape the backslash.
    Try "\\\\".
    � {�                                                                                                                                                                                                           

  • Having the '\' character in a string

    I get errors when I try to construct a string, say like this:
    String blahblah = new String("IBM\ROOT\IMAGE");
    Basically it seems like I can't store that '\' character in a string at all. Is this true?
    Thanks in advance.

    the '\' character is used to indicate an escape sequence, used for things like '\n' (new line). To do what you want you can do this:
    String blahblah = new String("IBM\\ROOT\\IMAGE"); //notice the double slash '\\' you would also have to do this for double and single quotes, e.g. ( new String("Bill said, \" Anne told me to \'go home now\'\"") : for... Bill said, "Anne told me to 'go home now'")

  • Find the count of the character in a string

    Hi,
    i have a scenario like i have one string in that character is repeated for more than one time.
    i want to know how many times that character is appeared in that string by using sql query.
    example: "Hello welcome to oracle world" In this i want to know how many time "e" is appeared..
    i think we can achieve by using instr and substr but i am not clear can any body please help me by using sql query to solve this issue

    regexp_count() function.
    Better answers available on the SQL-PLSQL forum. PL/SQL
    Edited by: SK1 on May 24, 2012 11:30 AM

  • How do count the character in the given string?

    hi all,
    i have astring like 'AAAARAMARAOAAA'.
    i want count how many 'A's in this string using sql statement.

    Is that a joke? You asked that in a previous thread of yours: how do count how many 'A' s in this string using sql stmt?.
    C.

  • Count a character within a string

    Give a string, what function can I use to count how many a particular character in the string. For example:
    "abc;def;ghi;xyz"
    I want to find out how many ";" in this string.
    Thank you

    And of course, if the researched character is not in the given string, then you'll return null, to return 0, you'll need a NVL.even if it is in the string you might need a nvl in Vamsi Kasina's solution:
    SQL>  select length (';') - length (replace (';', ';')) cnts
      from dual
          CNTS
    1 row selected.Better:
    SQL>  select nvl(length (';'),0) - nvl(length (replace (';', ';')),0) cnts
      from dual
          CNTS
             1

  • Count the number of occurrence in a string

    is there any api for counting the number of occurrence in a string or should i write a method myself?
    for example. I want to count "." in "1.2.3.4.". i want to count the dot in this string.
    I am just wondering.
    Thanks in advance.

    is there any api for counting the number of
    occurrence in a string or should i write a method
    myself?The latter.

  • Replacing a character in a string to another character

    hi,
    i need to write a function or procedure to replace the character of a string value suppose:
    l_error:= 'abcdefghijklmnop' is a string
    i need write a function or procedure to replace the character "c" to "z"
    that data in l_error is not in any table.
    thanks,
    AJ

    I want to Replace all the Existence of the word - "Test" in a string with "Test1" whereever a space exits before the word Test and someother alphabet after "Test" i.e. Test will be replaced with Test1 if a word starts with Test and contains more alphabets also. For example - TestName should be replaced with Test1Name while MyTest should not be updated to MyTest1.
    I have tried to use below query which uses oracle regular expressions -
    SELECT REGEXP_REPLACE('MYCOMPANY TEST TESTGEET INDIA PVT LTD TEST','\s(TEST)\w',' TEST1') FROM DUAL
    Output -
    "MYCOMPANY TEST *TEST1EET* INDIA PVT LTD TEST"
    Here, it has also replaced the G also from TESTGEET and resulted in TEST1EET while i want TEST1GEET.
    Can someone please suggest how can i do this..... may b m doing some silly mistake but sorry m a newbie to regular expression...
    Thanks in advance..

  • How to count a occurence of a string in file

    import java.util.*;
    import java.io.*;
    class Count
    public static void main(String args[ ] )throws Exception
         FileReader fr=new FileReader("d:\\compfetch\\clean\\first.txt");
         BufferedReader br=new BufferedReader(fr);
         StringBuffer sb=new StringBuffer();
         while((s=br.readLine())!=null)
              sb.append(s);
         s=sb.toString();
    Vector v= new Vector();
         v.addElement("job");
         v.addElement("jobs");
         v.addElement("career");
         v.addElement("careers");
         v.addElement("vacancy");
         v.addElement("vacancies");
         v.addElement("opportunity");
         v.addElement("openings");
         v.addElement("posting");
         v.addElement("postings");
         v.addElement("opportunities");
         v.addElement("placement");
         v.addElement("placements");
         v.addElement("job");
         v.addElement("job");
         Enumeration vEnum=v.elements();
         while(vEnum.hasMoreElements())
              System.out.println(vEnum.nextElement());
    I need to count the occurence of each string given in the vector from the text file.I have the file and the vector.How do i compare the vector and textfile ,so that i can count the occurence of eachString in the vector with the text file.The text file may contain any of the words given in the vector.If found ,a count shld be made for each string .
    regards,
    koel

    Try this code:
    import java.util.*;
    import java.io.*;
    class Count
    public static void main(String args[ ] )throws Exception
         FileReader     fr = new FileReader("c.c");
         BufferedReader br = new BufferedReader(fr);
         StringBuffer   sb = new StringBuffer();
         String         s;
         while((s=br.readLine())!=null)
              sb.append(s);
         s=sb.toString();
         Vector v= new Vector();
         v.addElement("job");
         v.addElement("jobs");
         v.addElement("career");
         v.addElement("careers");
         v.addElement("vacancy");
         v.addElement("vacancies");
         v.addElement("opportunity");
         v.addElement("openings");
         v.addElement("posting");
         v.addElement("postings");
         v.addElement("opportunities");
         v.addElement("placement");
         v.addElement("placements");
         Enumeration vEnum=v.elements();
         while(vEnum.hasMoreElements())
              String l = (String)vEnum.nextElement();
              int    a = 0;
              int    p = s.indexOf(l,a);
              int    c = 0;
              while (p != -1)
                   c = c + 1;
                   a = a + l.length() + p;
                   p = s.indexOf(l,a);
              System.out.println(l+"  "+c);
    }

  • Count the occurance of a character in a string

    What is the function to count the occurance of a character in a string.
    like 'test test1 test2 test3' with in this string there are 3 white spaces.
    instr will give the first one, replace will change the all... and what's for the occurance (number of white spaces, which is 3 in this case) count
    Message was edited by:
    gladnn

    SQL> var a varchar2(25)
    SQL> exec :a := 'gghhhh999jjjj'
    &nbsp
    PL/SQL procedure successfully completed.
    &nbsp
    SQL> select length(:a) - nvl(length(replace(:a,'h','')),0) from dual;
    &nbsp
    LENGTH(:A)-NVL(LENGTH(REPLACE(:A,'H','')),0)
                                               4Rgds.

  • Count the occurence of a character in a string

    Post Author: halfpat
    CA Forum: Formula
    Hello to everyone,
    I use CR 8,0. Is anyone can tell me if this is possible:
    I have a string "12345-C-1 23456-C-2 34567-C-4", how can I count the occurence of the "C" character in this string ?Is the InStr function can help me to do this ?
    I appreciate any suggestion,
    Thanks.

    Post Author: bettername
    CA Forum: Formula
    How about measuring the length of the string, and subtracting the length of the same string where C is replaced with <nothing>?
    len("12345-C-1 23456-C-2 34567-C-4") - len(replace("12345-C-1 23456-C-2 34567-C-4" , "C", ""))

  • Program to count the no. of times a character occurs in a string.

    program to count the no. of times a character occurs in a string.
    Say if u have a method :- Countclass(string s,char c) then using this u should be able to count a char ' T' in the string ="TESTING".
    So it is appering 2 times.
    How to count it.

    int countChar(String s, char c) {
       int count = 0;
       for (int i=0; i<s.length(); i++) {
          if (s.charAt(i) == c)
             count++;
       return count;
    }

Maybe you are looking for