How do I check to see if a value is an integer in MySQL?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In MySQL, "is this value an integer" can mean different things. You might be asking whether a column is defined with an integer type, whether a numeric expression has no fractional part, or whether a string looks like an integer literal.
Check the schema when the column type is the question
If the real question is "is this column an integer column," inspect the schema, not the row values.
If the result shows int, bigint, smallint, mediumint, or tinyint, then the column is integer-typed by schema definition.
This is the correct approach when you are auditing a table definition or generating dynamic SQL based on column metadata.
Check whether a numeric value is mathematically an integer
If the value is already numeric and you want to know whether it has a fractional part, compare it with a truncated form.
This works because values like 5 and 5.0 are still whole numbers mathematically, while 5.7 is not.
You can express the same idea with MOD:
That approach is fine when the input is already numeric.
Validate text values separately
If the value lives in a text column, numeric functions can be misleading because MySQL performs implicit conversion. For example, a string like '12abc' may be coerced to 12 in numeric contexts, which does not mean the original string is a valid integer literal.
For text validation, a regular expression is safer:
This checks whether the string is made of an optional sign followed by digits only.
That is usually what you want when cleaning imported CSV data or validating weakly typed input stored in VARCHAR.
Casting can help, but it is not validation by itself
Some people try to detect integers with casting:
This converts the value, but it does not prove the original text was clean. MySQL may still accept partially numeric strings. That is why regex-based validation is often better for textual input.
Use casting when you want conversion. Use regex when you want strict validation.
Handle NULL deliberately
Decide what NULL should mean in your application. It is not an integer value, but it may need to be treated separately from invalid text.
For example:
This preserves the difference between "missing value" and "value is present but not an integer."
Pick the right question before writing the query
A lot of confusion comes from trying to use one test for three different jobs:
- schema inspection
- numeric integrality
- string validation
Those are not the same problem, so they should not use the same solution. Once you define which one you actually need, the SQL becomes much clearer.
Common Pitfalls
The biggest mistake is applying numeric tests to strings and trusting MySQL’s implicit conversions. That can make invalid text appear valid because MySQL extracts a numeric prefix.
Another issue is treating 5.0 as non-integer just because it has a decimal representation. If the numeric value has no fractional part, it is still an integer mathematically.
Developers also forget the schema-versus-value distinction. A column can be typed as VARCHAR while containing text that looks like integers, or a numeric column can contain non-integer numeric values such as DECIMAL(10,2).
Finally, do not forget to define rules for signs, blanks, and NULL. Integer validation is only correct when those domain rules are explicit.
Summary
- Use
INFORMATION_SCHEMAwhen you want to know whether a column is integer-typed. - Use
FLOOR(value)orMOD(value, 1)when checking numeric values for a fractional part. - Use
REGEXPwhen validating whether text looks like an integer literal. - Do not rely on implicit numeric conversion for strict text validation.
- Decide up front whether you are checking schema, numeric value, or string content.

