
- 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
Get digits from a record in MySQL?
Use the function CONVERT() or Regular Expression. The CONVERT() method converts a value from one datatype to another. This will ecnetually fetch digits for us. Let us see an example.
Firstly, we will create a table.
mysql> create table textIntoNumberDemo -> ( -> Name varchar(100) -> ); Query OK, 0 rows affected (0.47 sec)
Inserting some records.
mysql> insert into textIntoNumberDemo values('John-11'); Query OK, 1 row affected (0.11 sec) mysql> insert into textIntoNumberDemo values('John-12'); Query OK, 1 row affected (0.17 sec) mysql> insert into textIntoNumberDemo values('John-2'); Query OK, 1 row affected (0.11 sec) mysql> insert into textIntoNumberDemo values('John-4'); Query OK, 1 row affected (0.14 sec)
To display all records.
mysql> select *from textIntoNumberDemo;
Here is the output.
+---------+ | Name | +---------+ | John-11 | | John-12 | | John-2 | | John-4 | +---------+ 4 rows in set (0.00 sec)
Syntax to fetch digit.
SELECT yourColumnName,CONVERT(SUBSTRING_INDEX(yourColumnName,'-',-1),UNSIGNED INTEGER) AS yourVariableName FROM yourTableName order by yourVariableName;
The following is the query.
mysql> SELECT Name,CONVERT(SUBSTRING_INDEX(Name,'-',-1),UNSIGNED INTEGER) AS MyNumber -> FROM textIntoNumberDemo -> order by MyNumber;
Here is the output.
+---------+----------+ | Name | MyNumber | +---------+----------+ | John-2 | 2 | | John-4 | 4 | | John-11 | 11 | | John-12 | 12 | +---------+----------+ 4 rows in set (0.00 sec)
- Related Articles
- How to get the second last record from a table in MySQL?
- Get the last record from a table in MySQL database with Java?
- How to get a specific column record from SELECT query in MySQL?
- How to get last 12 digits from a string in MySQL?
- Get the new record key ID from MySQL insert query?
- Get Random record from MongoDB?
- Get all digits from a string in Java
- Get only digits using regexp in MySQL?
- Get record count for all tables in MySQL database?
- Use IN() to get only a particular record in a MySQL stored procedure?
- Display record with today and tomorrow’s date from a column with date record in MySQL
- Get the returned record set order in MySQL IN clause?
- Get the record of a specific year out of timestamp in MySQL?
- How to get the top 3 salaries from a MySQL table with record of Employee Salaries?
- Get the sum of last 3 digits from all the values in a column with MySQL

Advertisements