How to count number of specific symbols in a row in MySQL?


You can use LENGTH() to count number of specific symbols in a row. Let us first create a table −

mysql> create table DemoTable
(
   Value varchar(200)
);
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('?1234?6789?5656?324?');
Query OK, 1 row affected (0.17 sec)

Following is the query to display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------------------+
| Value                |
+----------------------+
| ?1234?6789?5656?324? |
+----------------------+
1 row in set (0.00 sec)

Following is the query to count number of specific symbols in a row in MySQL −

mysql> SELECT LENGTH(Value) - LENGTH(REPLACE(Value,'?','')) from DemoTable;

This will produce the following output −

+-----------------------------------------------+
| LENGTH(Value) - LENGTH(REPLACE(Value,'?','')) |
+-----------------------------------------------+
| 5                                             |
+-----------------------------------------------+
1 row in set (0.03 sec)

Updated on: 30-Jul-2019

335 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements