tags:

views:

57

answers:

1

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') |
+----------------------------+
| 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)
A: 

Just thinking out loud, how about using regex?

SELECT '2a33434535' REGEXP '^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$';
KMan
@KMan, and how it helps? result is zero, but expected 233
Michael Pakhantsov