[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

find a date range with regular expression Oracle SQL

Posted on 2010-11-08
6
Medium Priority
?
600 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 2000 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

650 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