

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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.
<p style="">Let us first create a table −</p><pre class="result notranslate" style="">mysql> create table DemoTable ( Code varchar(100) ); Query OK, 0 rows affected (1.16 sec)</pre><p>Insert some records in the table using insert command −</p><pre class="result notranslate">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)</pre><p>Display all records from the table using select statement −</p><pre class="prettyprint notranslate">mysql> select *from DemoTable;</pre><p>This will produce the following output −</p><pre class="result notranslate" style="">+--------------------+ | Code | +--------------------+ | 100-677-9876 | | 100-677-9876-John | | David-100-677-9876 | +--------------------+ 3 rows in set (0.00 sec)</pre><p>Following is the Regex to display only numbers separated by hyphen −</p><pre class="prettyprint notranslate">mysql> select *from DemoTable where Code REGEXP '^\\(?[0-9]{3}\\)?[\\s-]?[0-9]{3}[\\s-]?[0-9]{4}$';</pre><p>This will produce the following output −</p><pre class="result notranslate" style="">+--------------+ | Code | +--------------+ | 100-677-9876 | +--------------+ 1 row in set (0.00 sec)</pre>
- Related Questions & Answers
- MySQL left padding with zeros separated by hyphen?
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- JavaScript regex program to display name to be only numbers, letters and underscore.
- How to display data from a MySQL column separated by comma?
- How to write Regex for numbers only in C#?
- How to concatenate string vectors separated with hyphen in R?
- MySQL query to remove numbers after hyphen in a VARCHAR string with numbers
- Display MySQL Results as comma separated list?
- Retrieve from MySQL only if it contains two hyphen symbols?
- Display all the column values in a single row separated by comma in MySQL?
- GROUP BY and display only non-empty column values in MySQL
- Regex to find string and next character in a comma separated list - MySQL?
- How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen
- How to set a string with hyphen and numbers in MySQL varchar?
- Split a column after hyphen in MySQL and display the remaining value?
Advertisements