Solved

find a date range with regular expression Oracle SQL

Posted on 2010-11-08
6
587 Views
Last Modified: 2012-05-10
create table parsetest (toparse varchar2 (100);

insert into parsetest values ('jan 3 2009 - dec 31 2009');
insert into parsetest values ('jan 3 2009-dec 31 2009');
insert into parsetest values ('feb 3 2009-dec 31 2009');
insert into parsetest values ('mar 3 2009-dec 31 2009');
insert into parsetest values ('apr 3 2009-dec 31 2009');
insert into parsetest values ('may 3 2009-dec 31 2009');
insert into parsetest values ('jun 3 2009-dec 31 2009');
insert into parsetest values ('jul 3 2009-');
insert into parsetest values ('3 jul 2009 - aug 31 2009, 1 oct 2009-');
insert into parsetest values ('3 aug 2009 - aug 31 2009, 1 oct 2009-31 dec 2009');
insert into parsetest values ('jan 2009');
insert into parsetest values ('feb 2009');

commit;

Open in new window


On an Oracle 11 database, I'm wanting a regular expression that returns all records that contain a range of data, these can be identified by the field having the following content:

a date : mmm d(d) yyyy
and a hyphen : the hyphen may come directly after the year but there may be one or more spaces preceding it.  The hyphen may also come before the date that matches the above pattern, for example see record 9 in the table.

Given the data in the table created above, I want the following data to be returned:

jan 3 2007 - dec 31 2007
jan 3 2007-dec 31 2007
feb 3 2007-dec 31 2007
mar 3 2007-dec 31 2007
apr 3 2007-dec 31 2007
may 3 2007-dec 31 2007
jun 3 2007-dec 31 2007
jul 3 2007-
3 jul 2007 - aug 31 2007, 1 oct 2007-
3 aug 2007 - aug 31 2007, 1 oct 2007-31 dec 2007

but not these:

jan 2009
feb 2009


How can this be achieved with a regular expression in an oracle SQL query?
0
Comment
Question by:yelbow
  • 2
  • 2
  • 2
6 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34086943
This seems to fit the above examples:
select * from parsetest where regexp_like (toparse, '(\d+\s+)?(j(an|u[nl])|feb|ma[ry]|apr|aug|sep|nov|dec)(\s*\d)?\s*\d{4}\s*-');

Open in new window

0
 
LVL 31

Expert Comment

by:awking00
ID: 34087060
>>Given the data in the table created above, I want the following data to be returned:<<
jan 3 2007 - dec 31 2007
etc.
Is it intended for 2009 to be replaced by 2007 or is that just a typo?
0
 

Author Closing Comment

by:yelbow
ID: 34087662
thanks!
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:yelbow
ID: 34087673
@awking00 - it was a typo, sorry!  Kaufmed's answer has done the trick though.
0
 
LVL 31

Expert Comment

by:awking00
ID: 34087703
I tried responding to this earlier, but my firewall keeps restricting me from posting. Just want to point out that the next to last "\d" in kaufmed's solution needs to be modified to "\d+" or "\d\d?" to accommodate dates like jan 13 ... (i.e. two-digit days).
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34087791
>>  kaufmed's solution needs to be modified to "\d+" or "\d\d?" ...

Yes it does. It seems I had a typo myself. Good eye  :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now