List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:April 21 1999 2:35am
Subject:Re: Is there a way to know if It's a number?
View as plain text  
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/
Thread
help! restoringpete collins19 Apr
  • Re: help! restoringChristian Mack20 Apr
    • Is there a way to know if It's a number?Jochen Haeberle20 Apr
      • Is there a way to know if It's a number?Michael Widenius21 Apr
        • Re: Is there a way to know if It's a number?Paul DuBois21 Apr
  • Re: Is there a way to know if It's a number?Benjamin Scherrey21 Apr
    • Re: Is there a way to know if It's a number?Thimble Smith21 Apr
  • Re: Is there a way to know if It's a number?Fred Read21 Apr
Re: Is there a way to know if It's a number?Christopher R. Jones21 Apr
  • Re: Is there a way to know if It's a number?Graham Ashton22 Apr