How to select only non - numeric values from varchar column in MySQL?

MySQLMySQLi Database

You need to use REGEXP for this. The syntax is as follows

SELECT *FROM yourTableName WHERE yourColumnName REGEXP '[a-zA-Z]';

To understand the concept, let us create a table. The query to create a table is as follows

mysql> create table SelectNonNumericValue
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> UserId varchar(100)
   -> );
Query OK, 0 rows affected (0.58 sec)

Insert some records in the table using insert command. The query is as follows

mysql> insert into SelectNonNumericValue(UserId) values('123John');
Query OK, 1 row affected (0.12 sec)
mysql> insert into SelectNonNumericValue(UserId) values('58475Carol98457Taylor24');
Query OK, 1 row affected (0.52 sec)
mysql> insert into SelectNonNumericValue(UserId) values('199575Sam124');
Query OK, 1 row affected (0.14 sec)
mysql> insert into SelectNonNumericValue(UserId) values('Mike2456');
Query OK, 1 row affected (0.14 sec)
mysql> insert into SelectNonNumericValue(UserId) values('1000');
Query OK, 1 row affected (0.12 sec)
mysql> insert into SelectNonNumericValue(UserId) values('1001');
Query OK, 1 row affected (0.21 sec)
mysql> insert into SelectNonNumericValue(UserId) values('10293Bob');
Query OK, 1 row affected (0.13 sec)
mysql> insert into SelectNonNumericValue(UserId) values('David');
Query OK, 1 row affected (0.30 sec)
mysql> insert into SelectNonNumericValue(UserId) values('2456');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using a select statement. The query is as follows

mysql> select *from SelectNonNumericValue;

The following is the output

+----+-------------------------+
| Id | UserId                  |
+----+-------------------------+
| 1 | 123John                  |
| 2 | 58475Carol98457Taylor24  |
| 3 | 199575Sam124             |
| 4 | Mike2456                 |
| 5 | 1000                     |
| 6 | 1001                     |
| 7 | 10293Bob                 |
| 8 | David                    |
| 9 | 2456                     |
+----+-------------------------+
9 rows in set (0.00 sec)

Here is the query to select non-numeric values

mysql> select *from SelectNonNumericValue WHERE UserId REGEXP '[a-zA-Z]';

The following is the output that ignores the numeric values

+----+-------------------------+
| Id | UserId                  |
+----+-------------------------+
| 1 | 123John                  |
| 2 | 58475Carol98457Taylor24  |
| 3 | 199575Sam124             |
| 4 | Mike2456                 |
| 7 | 10293Bob                 |
| 8 | David                    |
+----+-------------------------+
6 rows in set (0.00 sec)
raja
Published on 20-Mar-2019 10:14:11
Advertisements