4

我认为这很简单。我只想返回与查询结果中的前导数字联系的字符串值。

例如:

003 - Preliminary Examination Plan  
005 - Coordination  
1000a - Balance sheet
Advertising  
Amortization  
Partnerships

想得到:

003 - Preliminary Examination Plan  
005 - Coordination  
1000a - Balance sheet

这段代码给了我零结果。如何检查前导数字是否包含数字并返回字符串的其余部分?

select distinct AIssue
from SQLIssue
where regexp_like( AIssue, '^[[:digit:]]*$' )
order by AIssue
4

3 回答 3

6

您当前的正则表达式要求字符串完全由数字组成。尝试以下操作:

where regexp_like( AIssue, '^[[:digit:]].*$' )

(注意添加的点)。

详细地说,.匹配任何字符,*意思是“重复前一个词零次或多次”。

因此,原始正则表达式表示“零个或多个数字”,而上述正则表达式表示“一个数字后跟零个或多个任意字符

edit: A shorter version of the above regex has been suggested by @mellamokb in the comments:

where regexp_like( AIssue, '^[[:digit:]]' )
于 2012-05-24T15:45:48.367 回答
1

如果您使用的是 SQL Server,请尝试以下操作:

select distinct Issue
 from SQLIssue
 where AIssue LIKE '[0-9]%'
 order by AIssue

请参阅文档LIKE

于 2012-05-24T15:45:46.867 回答
1

Another solution, but one that doesn't involve regular expressions:

select distinct Issue
from SQLIssue
where substr(AIssue, 1, 1) between '0' and '9'
order by AIssue 
于 2012-05-24T15:55:29.433 回答