开发者

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*$';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜