4

I think this is fairly simple. I'd like to only return the string values that contact leading numbers in my query results.

For example:

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

Would like to get:

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

This code gave me zero results. How do I check if the leading numbers contain digits and return the rest of string?

select distinct AIssue
from SQLIssue
where regexp_like( AIssue, '^[[:digit:]]*$' )
order by AIssue
BvilleBullet
  • 201
  • 3
  • 8
  • 17

3 Answers3

6

Your current regex reqiures the string to consist entirely of digits. Try the following:

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

(note the added dot).

To elaborate, . matches any character, and * means "repeat the previous term zero or more times".

Thus, the original regex says "zero or more digits", whereas the above regex says "a digit followed by zero or more of any characters.

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

where regexp_like( AIssue, '^[[:digit:]]' )
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

If you're using SQL Server, try this:

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

See the documentation for LIKE

Daniel Renshaw
  • 33,729
  • 8
  • 75
  • 94
  • I tried this also before posting but it gave zero results I would assume because the entire string doesnt translate to a number. – BvilleBullet May 24 '12 at 15:52
  • 1
    The character range in `LIKE` clause, e.g., `[0-9]`, is a SQL Server-specific (and a couple other RDBMS's) feature: http://stackoverflow.com/questions/712580/list-of-special-characters-for-sql-like-clause. Since you're using an Oracle-based product, the query doesn't work. – mellamokb May 24 '12 at 15:56
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 
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786