Select all email addresses beginning with 5 numeric characters (regular expression) in MySQL


To get the email addresses beginning with 5 numeric characters, the optional solution is to use REGEXP −

select *from yourTableName where yourColumnName regexp "^[0-9]{5}";

Let us first create a table −

mysql> create table DemoTable
(
   UserEmailAddress varchar(100)
);
Query OK, 0 rows affected (0.76 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('6574John@gmail.com');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Carol23456@gmail.com');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('98989Chris_45678@gmail.com');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Mike12@gmail.com');
Query OK, 1 row affected (0.43 sec)
mysql> insert into DemoTable values('56453Adam@gmail.com');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------------------------+
| UserEmailAddress           |
+----------------------------+
| 6574John@gmail.com         |
| Carol23456@gmail.com       |
| 98989Chris_45678@gmail.com |
| Mike12@gmail.com           |
| 56453Adam@gmail.com        |
+----------------------------+
5 rows in set (0.00 sec)

Following is the query to select all email addresses beginning with 5 numeric characters −

mysql> select *from DemoTable where UserEmailAddress regexp "^[0-9]{5}";

This will produce the following output −

+----------------------------+
| UserEmailAddress           |
+----------------------------+
| 98989Chris_45678@gmail.com |
| 56453Adam@gmail.com        |
+----------------------------+
2 rows in set (0.00 sec)

Updated on: 07-Oct-2019

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements