How can I use MySQL OCTET_LENGTH() function to count the number of characters stored in a data column?


We need to pass the column name as the argument of OCTET_LENGTH() function to count the number of characters stored in a data column. It displays the number of characters when referenced in SELECT clause. It can also be used as comparison value to decide whether or not the row should returned by using it in WHERE clause. The contents of ‘Student’ table are used to demonstrate it −

mysql> Select Name, OCTET_LENGTH(Name)As 'Str_Length' from Student;
+---------+------------+
| Name    | Str_Length |
+---------+------------+
| Gaurav  | 6          |
| Aarav   | 5          |
| Harshit | 7          |
| Gaurav  | 6          |
| Yashraj | 7          |
+---------+------------+
5 rows in set (0.00 sec)

mysql> Select Name, OCTET_LENGTH(Name)As 'Str_Length' from Student Where OCTET_LENGTH(Name) < 7;
+--------+------------+
| Name   | Str_Length |
+--------+------------+
| Gaurav | 6          |
| Aarav  | 5          |
| Gaurav | 6          |
+--------+------------+
3 rows in set (0.06 sec)

Updated on: 07-Feb-2020

49 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements