
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- 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
- Regex to find string and next character in a comma separated list - MySQL?
- Retrieve from MySQL only if it contains two hyphen symbols?
- Display all the column values in a single row separated by comma in MySQL?
- Display MySQL Results as comma separated list?
- 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?
- GROUP BY and display only non-empty column values in MySQL

Advertisements