first 3 digits of a string
What is the best way to know the first 3 digits of a string? I have written a function for the same. It is working as expected, but I will like to know if there is a better way to do this.
mysql> select areacode_fun('233434535');
+---------------------------+
| areacode_fun('233434535') |
+---------------------------+
| 233 |
+---------------------------+
1 row in set (0.00 sec)
mysql> select areacode_fun('2 33434535');
+----------------------------+
| areacode_fun('2 33434535') |
+-----------开发者_JAVA百科-----------------+
| 233 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select areacode_fun('2a33434535');
+----------------------------+
| areacode_fun('2a33434535') |
+----------------------------+
| 233 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select areacode_fun('(2a3)3434535');
+------------------------------+
| areacode_fun('(2a3)3434535') |
+------------------------------+
| 233 |
+------------------------------+
1 row in set (0.00 sec)
Just thinking out loud, how about using regex?
SELECT '2a33434535' REGEXP '^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$';
精彩评论