简体   繁体   English

如何仅在第一个数字是从0到9的数字的位置选择行?

[英]How can I select rows only where first digit is a number from 0 to 9?

As far as I know I can do something like: 据我所知,我可以这样做:

"SELECT * 
 FROM my_table 
 WHERE my_field LIKE '0%' 
 OR my_field LIKE '1%' 
 OR my_field LIKE '2%' ";

Is there a way to achieve this with a regular expression or something like this: 有没有办法用正则表达式或类似的东西来实现这一点:

"SELECT * 
 FROM my_table 
 WHERE my_field LIKE [only first char is 0-9]"??

EDIT: The field is not numeric and it can be something like "1 People", "211 Pies" and so on. 编辑:该字段不是数字,它可以是“1人”,“211馅饼”等等。

Try this 试试这个

SELECT * 
FROM BadlyDesignedTable 
WHERE AnswerColumn RLIKE '^[0-9]+'

I was wondering if it was even possible to regex in where, found it on google in 30 seconds. 我想知道是否有可能在正确的地方,在30秒内在谷歌上找到它。

SELECT * FROM table WHERE field REGEXP '^[0-9]' 
Select * From my_table Where (REGEXP_LIKE(my_field, '[[:digit:]]%'))

The (REGEXP_LIKE(Source_String, '[[:character class:]]')) is a function you can use for numerous issues such as the one you have. (REGEXP_LIKE(Source_String, '[[:character class:]]'))是一个可用于许多问题的函数,例如你拥有的问题。 Simply use it to tell it to do that for the first digit. 只需用它来告诉它为第一个数字做那个。

http://docs.oracle.com/cd/B14117_01/server.101/b10759/conditions018.htm http://docs.oracle.com/cd/B14117_01/server.101/b10759/conditions018.htm

EDIT: Select * From my_table Where (SUBSTR(my_field,1,1) = '[[:digit:]]') Tried this in a similar query of mine, and it works. 编辑:选择*来自my_table其中(SUBSTR(my_field,1,1)='[[:digit:]]')在我的类似查询中尝试过此操作。

SELECT *
FROM my_table
WHERE left(my_field,1) REGEXP '^[0-9]+';

If you have MSSQL you can use 如果你有MSSQL,你可以使用

SELECT *
FROM my_table
WHERE isNumeric(left(my_field,1)) = 1

A simple one without regular expressions: 一个没有正则表达式的简单:

SELECT *, SUBSTR(my_field, 1, 1) AS mf FROM my_table HAVING mf BETWEEN '0' AND '9';

The quotes in the between clause are important! between子句中的引号很重要!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM