EN
MySQL - count characters in string (CHAR_LENGTH())
0 points
In this article, we would like to show you how to count characters in string using CHAR_LENGTH()
function in MySQL.
Quick solution:
xxxxxxxxxx
1
SELECT CHAR_LENGTH ('string');
In this example, we will count characters in dirask
string.
Query:
xxxxxxxxxx
1
SELECT CHAR_LENGTH ('dirask');
Result:

To show how to count distinct values, we will use the following table:

Note:
At the end of this article you can find database preparation SQL queries.
In this example, we will count the number of characters in name
column from users
table.
Query:
xxxxxxxxxx
1
SELECT
2
`id`, `name`,
3
CHAR_LENGTH (`name`) AS 'characters_in_name'
4
FROM `users`;
Output:

create_tables.sql
file:
xxxxxxxxxx
1
CREATE TABLE `users` (
2
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
3
`name` VARCHAR(50) NOT NULL,
4
`surname` VARCHAR(50) NOT NULL,
5
PRIMARY KEY (`id`)
6
);
7
insert_data.sql
file:
xxxxxxxxxx
1
INSERT INTO `users`
2
( `name`, `surname`)
3
VALUES
4
('John', 'Stewart'),
5
('Chris', 'Brown'),
6
('Kate', 'Lewis'),
7
('Ailisa', 'Gomez'),
8
('Gwendolyn', 'James'),
9
('Simon', 'Collins'),
10
('Taylor', 'Martin'),
11
('Andrew', 'Thompson');