How do I check to see if a value is an integer in MySQL?

Craig Nakamoto picture Craig Nakamoto · Sep 16, 2008 · Viewed 152.9k times · Source

I see that within MySQL there are Cast() and Convert() functions to create integers from values, but is there any way to check to see if a value is an integer? Something like is_int() in PHP is what I am looking for.

Answer

Jumpy picture Jumpy · Sep 16, 2008

I'll assume you want to check a string value. One nice way is the REGEXP operator, matching the string to a regular expression. Simply do

select field from table where field REGEXP '^-?[0-9]+$';

this is reasonably fast. If your field is numeric, just test for

ceil(field) = field

instead.