
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count All Characters in All Rows of a Field in MySQL
The syntax is as follows to count all characters in all rows of a field −
select sum(char_length(yourColumnName)) AS anyAliasName from yourTableName;
To understand the above syntax, let us create a table.
The query to create a table is as follows −
mysql> create table CountAllCharactersDemo -> ( -> UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> UserName varchar(20), -> UserSubject text -> ); Query OK, 0 rows affected (0.47 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into CountAllCharactersDemo(UserName,UserSubject) values('Larry','Introduction To Java'); Query OK, 1 row affected (0.19 sec) mysql> insert into CountAllCharactersDemo(UserName,UserSubject) values('Mike','Introduction To Computer Networks'); Query OK, 1 row affected (0.21 sec) mysql> insert into CountAllCharactersDemo(UserName,UserSubject) values('Sam','Introduction To C'); Query OK, 1 row affected (0.18 sec) mysql> insert into CountAllCharactersDemo(UserName,UserSubject) values('Carol','Introduction To Python'); Query OK, 1 row affected (0.25 sec) mysql> insert into CountAllCharactersDemo(UserName,UserSubject) values('David','Introduction To Spring And Hibernate Framework'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from CountAllCharactersDemo;
Here is the output −
+--------+----------+------------------------------------------------+ | UserId | UserName | UserSubject | +--------+----------+------------------------------------------------+ | 1 | Larry | Introduction To Java | | 2 | Mike | Introduction To Computer Networks | | 3 | Sam | Introduction To C | | 4 | Carol | Introduction To Python | | 5 | David | Introduction To Spring And Hibernate Framework | +--------+----------+------------------------------------------------+ 5 rows in set (0.00 sec)
Here is the query to count all characters in all rows of a field in MySQL.
Case 1 − Calculate total length.
The query is as follows −
mysql> select sum(char_length(UserSubject)) AS AllCharactersLength from CountAllCharactersDemo;
Here is the output −
+---------------------+ | AllCharactersLength | +---------------------+ | 138 | +---------------------+ 1 row in set (0.00 sec)
Case 2 − The query to calculate each row length −
mysql> select UserId,UserName,UserSubject,char_length(UserSubject) AS Length from CountAllCharactersDemo;
The following is The output −
+--------+----------+------------------------------------------------+--------+ | UserId | UserName | UserSubject | Length | +--------+----------+------------------------------------------------+--------+ | 1 | Larry | Introduction To Java | 20 | | 2 | Mike | Introduction To Computer Networks | 33 | | 3 | Sam | Introduction To C | 17 | | 4 | Carol | Introduction To Python | 22 | | 5 | David | Introduction To Spring And Hibernate Framework | 46 | +--------+----------+------------------------------------------------+--------+ 5 rows in set (0.00 sec)
Advertisements