Home » SQL & PL/SQL » SQL & PL/SQL » Count repeated characters in string
Count repeated characters in string [message #498221] Wed, 09 March 2011 05:02 Go to next message
obasad
Messages: 10
Registered: March 2006
Location: Riyadh
Junior Member

Hi ,
Is there any function to count number of specific character inside string.
for example :
my string is 'ABCEF AYZA'
I want to calculate how many 'A' exist in my string.
result should be = 3
regards
Re: Count repeated characters in string [message #498223 is a reply to message #498221] Wed, 09 March 2011 05:04 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Assuming 11g (you didn't specify) you can try Regexp_count
Re: Count repeated characters in string [message #498238 is a reply to message #498221] Wed, 09 March 2011 05:26 Go to previous messageGo to next message
S.Rajaram
Messages: 1027
Registered: October 2006
Location: United Kingdom
Senior Member
You can get the same result with the combination of length and replace.

Regards

Raj
Re: Count repeated characters in string [message #498270 is a reply to message #498221] Wed, 09 March 2011 07:24 Go to previous messageGo to next message
Manoj.Gupta.91
Messages: 239
Registered: March 2008
Location: Delhi
Senior Member
Hi,

One way to do this.
    SELECT LENGTH('ABCEF AYZA') - LENGTH(REPLACE('ABCEF AYZA','A')) 
    FROM DUAL ;


Second way to do this is using regular expressions. New feature introduced in 10g.
REGEXP_COUNT

http://psoug.org/reference/regexp.html

Regards
Manoj
Re: Count repeated characters in string [message #498273 is a reply to message #498270] Wed, 09 March 2011 07:48 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
manoj91 wrote on Wed, 09 March 2011 08:24
Hi,

One way to do this.


Close, but no cigar Cool. Your code does not handle strings that have nothing but search character:

SQL> SELECT LENGTH('AAAAAAAA') - LENGTH(REPLACE('AAAAAAAA','A')) N1,
  2         LENGTH('AAAAAAAA') - NVL(LENGTH(REPLACE('AAAAAAAA','A')),0) N2
  3    FROM DUAL
  4  /

        N1         N2
---------- ----------
                    8

1 row selected.

SQL> 


SY.
Re: Count repeated characters in string [message #498279 is a reply to message #498270] Wed, 09 March 2011 08:12 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
@manoj91

1/ The clue was given, let OP makes his homework himself
2/ REGEXP_COUNT way was also already given
3/ REGEXP_COUNT was not introduced in 10g but in 11g

Regards
Michel
Re: Count repeated characters in string [message #498412 is a reply to message #498279] Wed, 09 March 2011 21:20 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
much better you don't have to worry about character cases..

 SELECT  REGEXP_COUNT( 'bafewrdffaaaa','A', 1,'i' ) 
   FROM  DUAL
Re: Count repeated characters in string [message #498451 is a reply to message #498412] Thu, 10 March 2011 00:55 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Who says he has not to worry about? The question is clear and the solution and link were already given. If OP does not want to care about case he just have to read the link.

Regards
Michel
Re: Count repeated characters in string [message #498469 is a reply to message #498451] Thu, 10 March 2011 01:32 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
@michel Cadot

I'm just giving him another option. It's for him if he will use it or not. Did I violate a rule sorry if I did.

Is it not allowed here to post another approach to solve a problem?

[Updated on: Thu, 10 March 2011 01:58]

Report message to a moderator

Re: Count repeated characters in string [message #498482 is a reply to message #498469] Thu, 10 March 2011 02:15 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
This is not another approach, REGEXP_COUNT has already been posted twice before you.

Regards
Michel
Re: Count repeated characters in string [message #498487 is a reply to message #498279] Thu, 10 March 2011 02:52 Go to previous messageGo to next message
Manoj.Gupta.91
Messages: 239
Registered: March 2008
Location: Delhi
Senior Member
Hi,

@michel Cadot

I strongly agree with you that we should not do homework for others. We should give a hint or point him to right direction. The person must learn something.

As per my knowledge regular expressions are introduced in 10g. REGEXP_COUNT is introduced in 11g. Is my understanding correct?

I haven't worked on 11g. But I've used regular expressions on 10g little bit.

Can you point me to a knowledge base where I can find out the differences in 10g and 11g?

Thanks & Regards
Manoj


Re: Count repeated characters in string [message #498490 is a reply to message #498487] Thu, 10 March 2011 02:57 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
11gR2 new features

And yes, afaik that is correct

[Updated on: Thu, 10 March 2011 02:58]

Report message to a moderator

Re: Count repeated characters in string [message #498496 is a reply to message #498490] Thu, 10 March 2011 03:05 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
@michel cadot
yes but they did not post an example. so it is a rule here that you can't give examples if it is already mentioned because once you did michel cadot will get your attention.
Re: Count repeated characters in string [message #498497 is a reply to message #498496] Thu, 10 March 2011 03:08 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
My link had an example with,in I don't copy paste the because it encourages people to read about the functions - a typical example for a simple questions doesn't cover the full functionality of the more complex expressions.

Regexp can be a quagmire, which is why I pointed to the docs, you need to know what you're getting into Smile

[Updated on: Thu, 10 March 2011 03:09]

Report message to a moderator

Re: Count repeated characters in string [message #498504 is a reply to message #498497] Thu, 10 March 2011 03:17 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
I posted an example based from the problem given. Tell me roachcoach what is wrong about that? is because you already posted regexp_count other users can't post an example of that based from the problem?
Re: Count repeated characters in string [message #498508 is a reply to message #498504] Thu, 10 March 2011 03:20 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
What do you still ask this when we have answered to this same question in another topic of this forum.

Once again, read OraFAQ Forum Guide, the answer is there.
And yes, it is useless to repeat the same answer, in addition to NOT post a solution to a homework.

Regards
Michel
Re: Count repeated characters in string [message #498513 is a reply to message #498508] Thu, 10 March 2011 03:24 Go to previous messageGo to next message
xpact83
Messages: 225
Registered: October 2008
Location: philippines
Senior Member
is not stated there that you can't give an example if it is already mentioned.
Re: Count repeated characters in string [message #498515 is a reply to message #498504] Thu, 10 March 2011 03:26 Go to previous message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
xpact83 wrote on Thu, 10 March 2011 09:17
I posted an example based from the problem given. Tell me roachcoach what is wrong about that? is because you already posted regexp_count other users can't post an example of that based from the problem?


I didn't say there was a problem with it, you said there wasn't an example and I just said that there was within the link I gave and went on to explain (briefly) why I didn't copy it across - because regexp carry non trivial understanding requirements and can easily give the 'wrong' answer if gotten even slightly off. Trouble being, little tests usually work Wink

Don't take things so personally!
Previous Topic: From an Employee table, how will you display the record which has a maximum salary?
Next Topic: Truncate table
Goto Forum:
  


Current Time: Fri Aug 29 13:55:54 CDT 2025