user3790197
user3790197

Reputation: 11

how to count specific character in specific field SQL

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!

Upvotes: 0

Views: 267

Answers (1)

Alma Do
Alma Do

Reputation: 37365

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.

Upvotes: 3

Related Questions