i-CONICA
i-CONICA

Reputation: 2379

How to count all characters in all rows of a field in MySQL?

I need to character count the sum of all the characters in a text based field in MySQL. I need to know the total number of characters of all the posts in that field combined, and can't think how I'd do this...

Any help would be great.

Thanks.

Upvotes: 11

Views: 23178

Answers (3)

Rohan Khude
Rohan Khude

Reputation: 4883

CHAR_LENGH(column) counts with spaces. In account to count all characters expect spaces first we have to remove spaces. Hope this helps.

SELECT SUM(char_length(REPLACE(column, ' ', ''))) FROM table

Upvotes: 2

For the number of bytes . . .

select sum(length(your_column_name)) 
from your_table_name;

For the number of characters . . .

select sum(char_length(your_column_name)) 
from your_table_name;

The char_length() function accommodates multi-byte characters; five two-byte characters will return as 5.

Upvotes: 28

stivlo
stivlo

Reputation: 85476

Just sum all the lengths for the field

SELECT SUM(CHAR_LENGTH(field)) FROM table

Upvotes: 5

Related Questions