Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL to get only string with leading numbers

Tags:

regex

sql

oracle

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
like image 910
BvilleBullet Avatar asked Feb 20 '23 09:02

BvilleBullet


1 Answers

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:]]' )
like image 140
NPE Avatar answered Feb 28 '23 06:02

NPE