Count the number of occurrences of a string in a VARCHAR field-MySql

Author : Sachin Sharma
Published On : 22 Jun 2023
Tags: mysql

Here We will figure out how to calculate count of occurence of a character in a string or count the number of occurrences of a string in a VARCHAR field.

 

Lets explain with example

Case 1 : calculate how many time '-' is used in the string  ABC-CD-EF-10101.

 
SET @item='ABC-CD-EF-10101';
SELECT ROUND(   
        (
            LENGTH(@item)
            - LENGTH( REPLACE (@item,"-", "") ) 
        )        
    ) as Count;

Output : 3

Comments

No comments have been added to this article.

Add Comment