Select values that begin with a number

旧城冷巷雨未停 提交于 2019-11-28 09:45:50
SELECT * FROM YourTable WHERE YourColumn regexp '^[0-9]+'

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)

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 < ':'

also

SELECT * FROM YourTable
WHERE YourColumn LIKE '[0-9]%';
Vaibhav Kumar
SELECT * FROM TABLE T
WHERE T.COLUMNNAME REGEXP '^[0-9]';

Another answer is:

SELECT * FROM TABLE T
WHERE T.COLUMNNAME RLIKE '^[0-9]';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!