0

In a perfect world for this type of setup, we would have an integer column that expects only numbers;

But what if you have a varchar column and you want to add a WHERE clause that said something like this:

WHERE <value> is NOT a number

In essence, you are selecting all rows that contain any characters that are NOT ONLY numbers.

This is for MySQL.

TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259
  • 2
    This should answer your question [Detect if value is number in MySQL](http://stackoverflow.com/a/5065007/560299) – Ibu Oct 07 '13 at 17:16

4 Answers4

4

try this

    SELECT * FROM myTable WHERE concat('',col1 * 1) != col1

demo here

echo_Me
  • 37,078
  • 5
  • 58
  • 78
3

REGEXP or RLIKE are your friends:

SELECT * FROM `MyTable` WHERE `Column` RLIKE '^[^0-9]*$';

UPDv1:

You may use different regexes to detect negative integers:

SELECT
'-2' RLIKE '^[^0-9]*$', -- fails
'-1' RLIKE '-[0-9]';    -- succeeds

For example:

SELECT * FROM `MyTable` WHERE `Column` RLIKE '-[0-9]' OR `Column` RLIKE '^[^0-9]*$';

Tested with this:

SELECT 
    *
FROM
    (
        SELECT 'abs 12 x' as `Column`
        UNION ALL
        SELECT 12
        UNION ALL
        SELECT -2
        UNION ALL
        SELECT '-x'
    ) as `sub`
WHERE
    `Column` RLIKE '-[0-9]'
    OR
    `Column` RLIKE '^[^0-9]*$';

Output:

-2
-x
BlitZ
  • 12,038
  • 3
  • 49
  • 68
0

This would approach somehow your goal:

SELECT * FROM MyTable WHERE NOT MyTable.Field1 REGEXP '^[[:digit:]]*$';

As Field1 is VARCHAR, this will select all rows that Field1 is not wholly numerical.

If you have floating-point value:

SELECT * FROM MyTable WHERE NOT MyTable.Field1 REGEXP '^[[:digit:]\.]*$';
jacouh
  • 8,473
  • 5
  • 32
  • 43
0

You should be able to use a regular expression in the where clause.
The following mysql documentation link provides details:
http://dev.mysql.com/doc/refman/5.1/en/regexp.html

Seymour
  • 7,043
  • 12
  • 44
  • 51