Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Select only numeric values from varchar column

Consider the following table :

create table mixedvalues (value varchar(50));  insert into mixedvalues values  ('100'), ('ABC100'), ('200'), ('ABC200'), ('300'), ('ABC300'), ('400'), ('ABC400'), ('500'), ('ABC500'); 

How can I write a select statement that would only return the numeric values like

100 200 300 400 500 

SQLFiddle

like image 693
Wes Avatar asked Mar 19 '14 15:03

Wes


People also ask

How do I select a numeric value from a varchar column in SQL?

In SQL Server, we can use the ISNUMERIC() function to return numeric values from a column. We can alternatively run a separate query to return all values that contain numeric data.

How do I select only the value of an integer in MySQL?

Syntax to check if the value is an integer. select yourColumnName from yourTableName where yourColumnName REGEXP '^-?[0-9]+$'; The query wherein we have used regular expression. This will output only the integer value.

Can varchar have only numbers?

As the name suggests, varchar means character data that is varying. Also known as Variable Character, it is an indeterminate length string data type. It can hold numbers, letters and special characters.


1 Answers

SELECT *  FROM mixedvalues  WHERE value REGEXP '^[0-9]+$'; 
like image 181
Strawberry Avatar answered Sep 20 '22 03:09

Strawberry