- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
REGEX in MySQL to display only numbers separated by hyphen.
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)
Advertisements