Home >Database >Mysql Tutorial >How to Check if a Value is an Integer in MySQL?

How to Check if a Value is an Integer in MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-04 03:53:12165browse

How to Check if a Value is an Integer in MySQL?

Checking Whether a Value is an Integer in MySQL

In PHP, we can easily use the is_int() function to determine if a value is an integer. However, MySQL doesn't provide a direct equivalent.

Solution 1: Using REGEXP

For string values, the REGEXP operator can be used to match the value against a regular expression:

SELECT field FROM table WHERE field REGEXP '^-?[0-9]+$';

This regular expression ensures that the field matches a string that consists only of digits, with an optional negative sign at the beginning.

Solution 2: Using ceil()

If the field is numeric, we can check if it is an integer by comparing it to its rounded-up value:

SELECT field FROM table WHERE ceil(field) = field;

If the rounded-up value is equal to the original value, it means the value is an integer.

The above is the detailed content of How to Check if a Value is an Integer in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn