Months and days per month between two dates
murph2mMar 28 2012 — edited Apr 2 2012Hi,
I need to write a piece of code that when give two dates can calulate the months bewtenn the dates and for each month how many days are in - each month will be a record in a block.
Example
05/08/2011 - 16/10/2011
will give me 3 records to populate a block with..
AUG 2011 26 days
SEP 2011 30 days
OCT 2011 16 days.
Having searched around I've found some examples that with some fiddling I think I can get the results I need but they only work directly in SQL - I am unable to get them to work in a form..
The first example I've played with to start trying to get the number of days as well as the months...
with t as (SELECT TO_DATE('05/08/2011','DD/MM/RRRR') i_Date,
TO_DATE('16/10/2011','DD/MM/RRRR') f_Date from dual)
select TO_CHAR(ADD_MONTHS(i_Date,lvl),'MON'),TO_CHAR(ADD_MONTHS(i_Date,lvl),'RRRR'),
TO_CHAR(ADD_MONTHS(last_day(i_Date),lvl),'J') - TO_CHAR(ADD_MONTHS(i_Date,lvl),'J') days_in_first_month,
TO_CHAR(ADD_MONTHS(last_day(i_Date),lvl),'J') - TO_CHAR(ADD_MONTHS(trunc(i_Date,'MM'),lvl),'J') + 1 days_in_middle_months
FROM
(SELECT i_date,level - 1 lvl FROM t CONNECT BY level <= TRUNC(MONTHS_BETWEEN(f_Date,i_Date) + 1));
This example only returns the months at the moment..
select to_char(soeji,'mm'),to_char(soeji,'yyyy')
from dual
model
dimension by(trunc(date '2011-08-25','mm') as soeji)
measures(0 as dummy)
rules(
dummy[for soeji from trunc(date '2011-08-25','mm')
to date'2012-06-16'
increment InterVal '1' month]
= 0);
I think if I could get either of these to work within a form so that I use the results to populate a block then that would be great or any alternative ideas very welcome!
Thanks in advance
Matt