List:General Discussion« Previous MessageNext Message »
From:Visolve DB Team Date:October 11 2006 5:36am
Subject:Re: Counting char in a column
View as plain text  
Hi,

MySQL dosen't have built-in function for counting substring.  But we can 
create user-defined functions for this.
Like,

CREATE FUNCTION substrCount(x varchar(255), delim varchar(12)) returns int
return (length(x)-length(REPLACE(x, delim, '')))/length(delim);

Then try,

SELECT substrCount('Characteristics', 'c') as count;
which returns 3.

For more reference 
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

Thanks,
ViSolve DB Team.
----- Original Message ----- 
From: "Scott Hamm" <linuxgold@stripped>
To: "'Mysql '" <mysql@stripped>
Sent: Tuesday, October 10, 2006 10:58 PM
Subject: Counting char in a column


> I'm running MySQL 5.0.15 on Windows system.
>
> How do I count how many specific char is there in a column, for example
> finding 'c' in lowercase string of "Characteristics" would total to 3.
>
>
>
>
> -- 
> --------------------------------------------------------------------
> `Twas brillig, and the slithy toves
> Did gyre and gimble in the wabe:
> All mimsy were the borogoves,
> And the mome raths outgrabe.
> --------------------------------------------------------------------
> 

Thread
Counting char in a columnScott Hamm10 Oct
  • Re: Counting char in a columnVisolve DB Team11 Oct