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

Similar Messages

  • 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 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 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>

  • How to remove /delete a particular character in a String

    Hii
    i have this problem.In order to specify the number of decimal palces for a double type variable,i have used the
    NumberFormat class.
    My code ;
    Double x=234566.4 ;
    NumberFormat n1= NumberFormat.getInstance();
    n1.setMaximumFractionDigits(2);
    n1.setMinimumFractionDigits(2);
    String st=n1.format(x);
    out.print(st) ;
    is giving me 2,34,566.40 which I dont want I want to have it lik 234566.40
    since i will again put it in function Integer.parseInt/Double.parseDouble...so those commas are giving me errors.So can anybody suggest how to remove those commas from that string...It would be of gr8 help.thanx
    Arnab

    hi
    You can use StringTokenizer
    Your code
    Double x=234566.4 ;
    NumberFormat n1= NumberFormat.getInstance();
    n1.setMaximumFractionDigits(2);
    n1.setMinimumFractionDigits(2);
    String st=n1.format(x);
    out.print(st) ;
    StringTokenizer sT = new StringTokenizer(st,",");
    String newstring="";
    while(sT.hasMoreTokens())
    newstring += sT.nextToken();
    out.println(newstring);
    this may help out
    cheers
    rambee

  • To find out the POSITION OF Nth OCCURANCE OF A PARTICULAR CHARACTER

    Hi,
    Is it possible to find out the POSITION OF Nth OCCURANCE OF A PARTICULAR CHARACTER IN A STRING in crystal reports?
    Eg:
    I have a string abcdabcdabcd
    Now I want to know the position of the 2nd occurance of u2018au2019 in the string u2018abcdabcdabcdu2019

    hi,
    dim MainStr as String=u2018abcdabcdabcdu2019
    dim SubStr as String='a' 'Give which alphabet do you want.
    dim Num as integer=2 'Give which 'a' do you want. i gave for second a.
    dim ResultStr as string
    dim ResultNum as integer=1
    dim Position as integer=0
    for i as integer=0 to MainStr.length-1
    ResultStr=MainStr.SubString(i,1)
    if ResultStr=SubStr then
    ResultNum+=1
    if ResultNum=Num then
    position=i+1
    exitfor
    endif
    endif
    next
    hope this solves your problem
    regards,
    varma

  • Replace a character from a string

    Hi,
    how can I replace a particular character from a string on a particular occurrence? Say for eg, if i wan to replace 'a' of second occurrence from 'abcabcabc', then what should i do? I guess the normal REPLACE function replaces every occurrence in a given string. So is there any other way to solve this?
    Thanks!

    If you are in 9i:
    Then you got to split the string into based on the nth position.
    SQL> select replace('abddefabc','a','x') from dual;
    REPLACE('
    xbddefxbc
    SQL> select instr('abddefabc','a',2) from dual;
    INSTR('ABDDEFABC','A',2)
                           7
    SQL> select substr('abddefabc',1,instr('abddefabc','a',2)-1),substr('abddefabc',instr('abddefabc','a',2)) from dual;
    SUBSTR SUB
    abddef abc
    SQL> select substr('abddefabc',1,instr('abddefabc','a',2)-1),replace(substr('abddefabc',instr('abddefabc','a',2)),'a','X') as replaced from dual;
    SUBSTR REP
    abddef XbcOfcourse, later on you can join the string..
    Jithendra

  • How to count number of occurences of a character  in a string

    I want to get the count of occurences of a character within a string. Is there any function for this or should i go for a PLSQL.
    For example is the sting is "occurences" & if i search for character "c", the answer should be 3.
    Regards,
    Sunil.

    SQL> select length('occurence') - length(replace('occurence','c')) from dual;
    LENGTH('OCCURENCE')-LENGTH(REPLACE('OCCURENCE','C'))
    3

  • Count the repeated character in a string

    Hi Great Gurus,
    Please tell me how to know how many times a given character repeats in a string.
    Please mail me .
    Thanks Gurus,
    Rahul

    hi,
    same thread----
    Re: find number of occurances of a particular character
    data:  var1(30)  type c value 'ghghj#ghjgjgh#ghjghjg#ghjg#'.
    data : totcnt type i,
           cnt type i,
           v type c,
           n type i.
    cnt = strlen( var1 ).
    do cnt times.
    move var1+n(1) to v.
    if v eq '#'.
    totcnt = totcnt + 1.
    endif.
    n = n + 1.
    if n = cnt .
    exit.
    endif.
    enddo.
    write:/ 'No of #s', totcnt .
    Regards
    Reshma

  • 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", ""))

  • Looking for the last item in a string set in a particular character style

    I should find each space that is the last character in a string set in a particular character style. For example, in the passage "123 456 789" (the underlined part here indicating a passage set in a particular character style), my ideal GREP search would yield a match after the digit 6.
    How do this?

    Excellent! This works perfectly. I have used your string with the bold character formatting, and replaced the matches with [nothing], set in None style.
    On second thought, I was surprised that it indeed had worked: why was the space replaced, and not deleted? Shouldn't I have used
    (?<!.)(\s)(?=\w)
    and replaced it with
    $2

  • Which method to count the number of character in a string?

    Hi all,
    I want to know which class and method to count the number of character in a string?
    Gary

    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

  • Counting character in a string

    I'm trying to count how many times a characters occurs in a string, and I'm having a lot of trouble. This is what I have so far:
    note: the method call has to be public static int countOccs(char f, String s) and I know that it can be done using only loops, for, charAt, and length () and I want to learn it like that.
    [I just chose f, s randomly.]
    public class Counting {
    public static void main(String [ ] args) {
    String s = "Testing";
    int countOccs = s.length( );
    char [ ] charArray = new new char[counting];
    for (int i = 0; i < counting; i++) {
    charArray[i]
    public static int counting(char f, String s) {
    I'm really lost. When I try to define char f = "f" or String s = "Testing" in the method call (public static int counting...) I get messages saying that it has already been declared in the call, but there has been nothing assigned to it. Anyways, I feel really lost. I did find a pattern by looking at my notes from awhile ago, but am having trouble filling out parts of it. (Thanks, memory...) I think the problem comes from having to have the method call be public static int counting (char f, String s). That part confuses me a lot because I want it to print out how many characters of f are in a string, but it doesn't have "void" in it so it doesn't return a value.
    for (int i=0; i < s.length(); i++)
    char f = s.charAt(i);
    if (something that looks at the characters)
    do something like count
    Edited by: MAresJonson on Feb 6, 2009 6:50 PM

    h1. The Ubiquitous Newbie Tips
    * DON'T SHOUT!!!
    * Homework dumps will be flamed mercilessly. [Feelin' lucky, punk? Well, do ya'?|http://www.youtube.com/watch?v=1-0BVT4cqGY]
    * Have a quick scan through the [Forum FAQ's|http://wikis.sun.com/display/SunForums/Forums.sun.com+FAQ].
    h5. Ask a good question
    * Don't forget to actually ask a question. No, The subject line doesn't count.
    * Don't even talk to me until you've:
        (a) [googled it|http://www.google.com.au/] and
        (b) had a squizzy at the [Java Cheat Sheet|http://mindprod.com/jgloss/jcheat.html] and
        (c) looked it up in [Sun's Java Tutorials|http://java.sun.com/docs/books/tutorial/] and
        (d) read the relevant section of the [API Docs|http://java.sun.com/javase/6/docs/api/index-files/index-1.html] and maybe even
        (e) referred to the JLS for "advanced" questions.
    * [Good questions|http://www.catb.org/~esr/faqs/smart-questions.html#intro] get better Answers. It's a fact. Trust me on this one.
        - Lots of regulars on these forums simply don't read badly written questions. It's just too frustrating.
          - FFS spare us the SMS and L33t speak! Pull your pants up, and get a hair cut!
        - Often you discover your own mistake whilst forming a "Good question".
        - Often you discover you discover you where trying to answer "[the wrong question|http://blog.aisleten.com/2008/11/20/youre-asking-the-wrong-question/]".
        - Many of the regulars on these forums will bend over backwards to help with a "Good question",
          especially to a nuggetty problem, because they're interested in the answer.
    * Improve your chances of getting laid tonight by writing an SSCCE
        - For you normal people, That's a: Short Self-Contained Compilable (Correct) Example.
        - Short is sweet: No-one wants to wade through 5000 lines to find your syntax errors!
        - Often you discover your own mistake whilst writing an SSCCE.
        - Often you solve your own problem whilst preparing the SSCCE.
        - Solving your own problem yields a sense of accomplishment, which makes you smarter ;-)
    h5. Formatting Matters
    * Post your code between a pair of &#123;code} tags
        - That is: &#123;code} ... your code goes here ... &#123;code}
        - This makes your code easier to read by preserving whitespace and highlighting java syntax.
        - Copy&paste your source code directly from your editor. The forum editor basically sucks.
        - The forums tabwidth is 8, as per [the java coding conventions|http://java.sun.com/docs/codeconv/].
          - Indents will go jagged if your tabwidth!=8 and you've mixed tabs and spaces.
          - Indentation is essential to following program code.
          - Long lines (say > 132 chars) should be wrapped.
    * Post your error messages between a pair of &#123;code} tags:
        - That is: &#123;code} ... errors here ... &#123;code}
        - OR: &#91;pre]&#123;noformat} ... errors here ... &#123;noformat}&#91;/pre]
        - To make it easier for us to find, Mark the erroneous line(s) in your source-code. For example:
            System.out.println("Your momma!); // <<<< ERROR 1
        - Note that error messages are rendered basically useless if the code has been
          modified AT ALL since the error message was produced.
        - Here's [How to read a stacktrace|http://www.0xcafefeed.com/2004/06/of-thread-dumps-and-stack-traces/].
    * The forum editor has a "Preview" pane. Use it.
        - If you're new around here you'll probably find the "Rich Text" view is easier to use.
        - WARNING: Swapping from "Plain Text" view to "Rich Text" scrambles the markup!
        - To see how a posted "special effect" is done, click reply then click the quote button.
    If you (the newbie) have covered these bases *you deserve, and can therefore expect, GOOD answers!*
    h1. The pledge!
    We the New To Java regulars do hereby pledge to refrain from flaming anybody, no matter how gumbyish the question, if the OP has demonstrably tried to cover these bases. The rest are fair game.

  • Counting a particular word in a string

    Hi All,
    Have a query to find the occurence of a particular word in a string using a query.
    ex: str := 'a,b,a,c,d'
    search_str := 'a'
    => need to get the number of times 'a' is getting repeated in the string str.
    output: 2
    Hoping for the best support as always.
    Edited by: Aparna16 on Jul 17, 2012 5:55 AM
    Edited by: Aparna16 on Jul 17, 2012 5:56 AM

    SQL> ed
    Wrote file afiedt.buf
      1  with sample_data as
      2  (
      3  select 'aaa,abcdd,abc,abc,asdasd' cola, 'abc' matchCol from dual union all
      4  select 'a,b,a,c,d', 'a' from dual union all
      5  select 'a,b,a,c,d', 'e' from dual  union all
      6  select  'aaa,abcdd,abc,abc,asdasd,abc', 'ab' from dual union all
      7  select  'aaa,abcdd,abc,abc,asdasd,abc', 'abc' from dual
      8  )
      9  select cola, matchcol, length(cola||',') - length(replace(cola||',',matchcol||',',substr(matchcol,2)||','))  cnt
    10* from sample_data
    SQL> /
    COLA                         MAT        CNT
    aaa,abcdd,abc,abc,asdasd     abc          2
    a,b,a,c,d                    a            2
    a,b,a,c,d                    e            0
    aaa,abcdd,abc,abc,asdasd,abc ab           0
    aaa,abcdd,abc,abc,asdasd,abc abc          3

Maybe you are looking for