Reputation: 9158
I have a table with a column containing data that begin with numbers too, on MySQL
How can I select the rows that begin only with a number?
Upvotes: 27
Views: 142836
Reputation: 544
SELECT * FROM TABLE T
WHERE T.COLUMNNAME REGEXP '^[0-9]';
Another answer is:
SELECT * FROM TABLE T
WHERE T.COLUMNNAME RLIKE '^[0-9]';
Upvotes: 2
Reputation: 115520
Yet another way:
WHERE LEFT(columnName,1) IN ('0','1','2','3','4','5','6','7','8','9')
and with common charsets and collations, this would work and use an index on the column:
WHERE columnName >= '0' AND columnName < ':'
Upvotes: 13
Reputation: 454960
You can do:
SELECT *
FROM MyTable
WHERE MyColumn REGEXP '^[0-9]';
The regular expression used is ^[0-9]
.
^ - Start anchor, used to ensure the pattern matches start of the string.
[ - Start of character class.
0-9 - Any digit
] - End of character class
Effectively we are trying to select those values in the column that begin with a digit.
Demo:
mysql> select * from tab;
+-------+
| col |
+-------+
| 1foo |
| foo |
| 10foo |
| foo10 |
+-------+
4 rows in set (0.00 sec)
mysql> select * from tab where col regexp '^[0-9]';
+-------+
| col |
+-------+
| 1foo |
| 10foo |
+-------+
2 rows in set (0.00 sec)
Upvotes: 10