Solved

find a date range with regular expression Oracle SQL

Posted on 2010-11-08
6
594 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 75

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 32

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

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 75

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

739 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