Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to automate the date range 1

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I am using a PL/SQL script to extract data and I am currently using,

Code:
   and I.MATCH_DATE    >= to_date('09/01/2011', 'MM/DD/YYYY')
   and I.MATCH_DATE     < to_date('10/01/2011', 'MM/DD/YYYY')

The second month is the current month and the starting month is last month. Can this be coded to automatically generate the dates?
 
Something like:

Code:
select trunc(add_months(sysdate,-1), 'mm')+8,
       trunc(sysdate, 'mm') + 9
 from (select to_date('01/10/2011', 'DD/MM/YYYY') as match_date from dual) i
where I.MATCH_DATE    >= trunc(add_months(sysdate,-1), 'mm')+8
   and I.MATCH_DATE     < trunc(sysdate, 'mm') + 9

 
I guess I communicated poorly, I am trying to get the current date and compute the first day of the preivous month and the first day of this month.

I was running into issues where the current month is January
 
That's even easier. Just use:

trunc(add_months(sysdate,-1), 'mm')
trunc(sysdate, 'mm')

 
Will this give me the first day of each month?

For example today is 10/24/2011. I need the first date to be 9/1/2011 and the second to be 10/1/2011. This way I am testing for all values greater than or equal to 9/1/2011 and less than 10/1/2011. This way I don't have to worry about what the last day of the previous month was.
 
Yes, trunc(<date>, 'mm'> always gives the first day of each month.

 
However if you did want to check between the start and end dates of the last month then

between trunc(add_months(sysdate,-1),'mm') and last_day(trunc(add_months(sysdate,-1)))

Bill
Lead Application Developer
New York State, USA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top