2

How can I select rows in MySQL that are not numbers?

 SELECT * 
 FROM `TABLE`
 WHERE `Column_B` <> Number

I know, this is a stupid question, but I haven't found a solution for this. Column_B is varchar(3).

Peter
  • 1,224
  • 3
  • 16
  • 28

3 Answers3

6

Try this sqlfiddle..

http://sqlfiddle.com/#!2/17f28/1

SELECT * FROM Table1 WHERE col NOT REGEXP '^[0-9]+$'


If you want to match real numbers (floats) rather than integers, you need to handle the case above, along with cases where your pattern is between 0 and 1 (i.e. 0.25), as well as case where your pattern has a decimal part that is 0. (i.e. 2.0). And while we're at it, we'll add support for leading zeros on integers (i.e. 005):

 ^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$

sql looks like SELECT * FROM Table1 WHERE col NOT REGEXP '^0*[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$'

Nisar
  • 5,708
  • 17
  • 68
  • 83
0

This is one way of doing that, using mysql regexp operator (fiddle):

SELECT * FROM `TABLE`
WHERE `Column_B` NOT REGEXP '^-?[0-9]*\\.?[0-9]+([Ee][+-][0-9]+)?$';

Supports signed/unsigned integers, decimals, and scientific notation.

Note: it is recommended to avoid using sql keywords for entity names, even if you quote them with backticks.

guido
  • 18,864
  • 6
  • 70
  • 95
-2

this also will work for you

   SELECT col  FROM Table1 
  WHERE col NOT IN ( SELECT col FROM Table1
                   where CAST(col as DECIMAL(10,5)) !=0
                   or Col = '0' )

DEMO

echo_Me
  • 37,078
  • 5
  • 58
  • 78
  • 1
    question is .. select rows that are not numbers. why you are selecting numbers. –  May 25 '14 at 10:21
  • @radha edited with non numbers and decimal. thanks for catch :) – echo_Me May 25 '14 at 10:26
  • 1
    this fails when input string has digits followed by characters like `1aa` or `3--`. http://sqlfiddle.com/#!2/b80efb/1/0 – guido May 25 '14 at 11:12