List:General Discussion« Previous MessageNext Message »
From:Michael Widenius Date:April 20 1999 10:30pm
Subject:Is there a way to know if It's a number?
View as plain text  
>>>>> "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)

mysql> select "1242a" REGEXP "^[0-9]*$";
+---------------------------+
| "1242a" REGEXP "^[0-9]*$" |
+---------------------------+
|                         0 |
+---------------------------+
1 row in set (0.00 sec)


Fixed number floats can be checked with:

mysql> select "1242.4" REGEXP "^[0-9]*(\\.[0-9]+){0,1}$";

Regards,
Monty
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