Problem, Part II: Most online resources show how you can find out if part of your string is numeric.
Solution:
SELECT str FROM tblGive it a whirl!
WHERE str REGEXP('(^[0-9]+$)');
For those who would like to view some test results, here goes:
mysql> SELECT str FROM tbl;
+-----+
| str |
+-----+
| 10 |
| 10A |
| 10E |
| 78 |
| E78 |
+-----+
5 rows in set (0.00 sec)
mysql> SELECT str FROM tbl WHERE str REGEXP('(^[0-9]+$)');Now for the other way around:
+-----+
| str |
+-----+
| 10 |
| 78 |
+-----+
2 rows in set (0.00 sec)
mysql> SELECT str FROM tbl WHERE str NOT REGEXP('(^[0-9]+$)');
+-----+
| str |
+-----+
| 10A |
| 10E |
| E78 |
+-----+
3 rows in set (0.00 sec)
5 comments:
The full regex for php's is_numeric is:
REGEXP('(^[+-]?[0-9]+\.?[0-9]*e?[0-9]+$)|(0x[0-9A-F]+)')
As it supports more then just ints
Whoops, that should be:
REGEXP('^([+-]?[0-9]+\.?[0-9]*e?[0-9]+)|(0x[0-9A-F]+)$')
That way it only matches a full hex value
Couldn't find any resources on how to test if part of a string is numeric - how would you test if the first two characters of a string were numeric?
SELECT str FROM tbl WHERE LEFT(str,2) REGEXP '[0-9]+';
Thanks =)
Post a Comment