At 5:30 PM -0500 4/20/1999, Michael Widenius wrote:
>>>>>> "Jochen" == Jochen Haeberle <listen@stripped> writes:
>
>Jochen> Hi,
>Jochen> is there a way to check if a value is a number?
>Jochen> Something like SELECT isNumber(string)?
>
>Jochen> I am having problems at my website the people are sometimes entering
>Jochen> strings into fields that will be calculated with later on in MySQL
>Jochen> (currency units). I know there are other solutions to checking if a
>Jochen> value is a number (including JavaScript), but I do not want to use
>Jochen> another solution just for this.
>
>Jochen> Thanks in advance
>
>Jochen> Jochen
>
>Jochen> ...and I did look into the maual first...
>
>Hi!
>
>You can check for a integer with REGEXP. Here is an example of this:
>
>mysql> select "1242" REGEXP "^[0-9]*$";
>+--------------------------+
>| "1242" REGEXP "^[0-9]*$" |
>+--------------------------+
>| 1 |
>+--------------------------+
>1 row in set (0.19 sec)
Yes, but:
mysql> select "" REGEXP "^[0-9]*$";
+----------------------+
| "" REGEXP "^[0-9]*$" |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.05 sec)
So if it's important not to treat the empty string as a number,
this might be better:
mysql> select "" REGEXP "^[0-9]+$";
+----------------------+
| "" REGEXP "^[0-9]+$" |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.00 sec)
--
Paul DuBois, paul@stripped
Northern League Chronicles: http://www.snake.net/nl/