Let us first create a table −
mysql> create table DemoTable ( Code varchar(100) ); Query OK, 0 rows affected (1.16 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('100-677-9876'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values('100-677-9876-John'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David-100-677-9876'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------------------+ | Code | +--------------------+ | 100-677-9876 | | 100-677-9876-John | | David-100-677-9876 | +--------------------+ 3 rows in set (0.00 sec)
Following is the Regex to display only numbers separated by hyphen −
mysql> select *from DemoTable where Code REGEXP '^\\(?[0-9]{3}\\)?[\\s-]?[0-9]{3}[\\s-]?[0-9]{4}$';
This will produce the following output −
+--------------+ | Code | +--------------+ | 100-677-9876 | +--------------+ 1 row in set (0.00 sec)