Select All Email Addresses Beginning with 5 Numeric Characters 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('[email protected]');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('[email protected]');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('[email protected]');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('[email protected]');
Query OK, 1 row affected (0.43 sec)
mysql> insert into DemoTable values('[email protected]');
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           |
+----------------------------+
| [email protected]         |
| [email protected]       |
| [email protected] |
| [email protected]           |
| [email protected]        |
+----------------------------+
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           |
+----------------------------+
| [email protected] |
| [email protected]        |
+----------------------------+
2 rows in set (0.00 sec)
Updated on: 2019-10-07T12:41:17+05:30

201 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements