33

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

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Wes
  • 894
  • 1
  • 7
  • 17

6 Answers6

111
SELECT * 
FROM mixedvalues 
WHERE value REGEXP '^[0-9]+$';
potashin
  • 44,205
  • 11
  • 83
  • 107
Strawberry
  • 33,750
  • 13
  • 40
  • 57
9
SELECT * 
FROM mixedvalues 
WHERE concat('',value * 1) = value;

Reference: Detect if value is number in MySQL

Community
  • 1
  • 1
Slowcoder
  • 2,060
  • 3
  • 16
  • 21
4

You were close :

SELECT * 
FROM mixedvalues 
WHERE value > 0;

SQLFiddle

potashin
  • 44,205
  • 11
  • 83
  • 107
Patrick Q
  • 6,373
  • 2
  • 25
  • 34
1
SELECT * FROM mixedvalues 
WHERE value > 0 
ORDER BY CAST(value as SIGNED INTEGER) ASC
Jit
  • 31
  • 1
  • 3
1

List item has string continue with numbers

$string = "Test";

select * from table where columnname REGEXP "$string-*[0-9]+"; 
Sreejith N
  • 25
  • 5
-4

You can filter your result set using the ISNUMERIC function:

SELECT value
FROM #mixedvalues 
where ISNUMERIC(value)=1
trincot
  • 317,000
  • 35
  • 244
  • 286