0

I want to make a query in MySQL that counts the number of appearances of a character in specific field

for example:

Class          | ID 
============================
d              |1          
c;g;i;m        |2          
r;e            |3          

final resault should be:

Class          | ID        | NumOf; (to be added)
==========================================================
d              |1          | 0
c;g;i;m        |2          | 3
r;e            |3          | 1

thank you!

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • 1
    possible duplicate of [Count the number of occurences of a string in a VARCHAR field?](http://stackoverflow.com/questions/12344795/count-the-number-of-occurences-of-a-string-in-a-varchar-field) – Jordan.J.D Jun 30 '14 at 12:50

1 Answers1

3

So that is with REPLACE():

SELECT CHAR_LENGTH(Class) - CHAR_LENGTH(REPLACE(Class, ';', '')) AS result FROM t

That is: count how many characters are before replacement and after.

Alma Do
  • 37,009
  • 9
  • 76
  • 105