Need Help on List of Months between Two Dates
Hello everyone,
I have a table which has startdate & enddate, and need a SELECT statement to list all months between these two dates for each given ID.
I did some test, and could not figure out how to get the startdate & enddate from testing table (instead of hard code them) in the select statement.
Could anybody please help on it (Oracle 11gR2),
Thanks in advance!!!
create table testing(
id number,
start_date date,
end_date date);
insert into testing values(100, to_date('05-FEB-2011', 'DD-MON-YYYY'), to_date('28-MAY-2011', 'DD-MON-YYYY'));
insert into testing values(200, to_date('20-JUN-2011', 'DD-MON-YYYY'), to_date('28-DEC-2011', 'DD-MON-YYYY'));
commit;
select * from testing;
ID START_DAT END_DATE
100 05-FEB-11 28-MAY-11
200 20-JUN-11 28-DEC-11
Elapsed: 00:00:00.01
*for testing.id = 100:*
select to_char(add_months(to_date('05-FEB-2011', 'DD-MON-YYYY'), l - 1), 'YYYY-Mon') Dates
from (select level l
from dual
connect by level <= months_between(trunc(to_date('28-MAY-2011', 'DD-MON-YYYY'), 'MONTH'),
trunc(to_date('05-FEB-2011', 'DD-MON-YYYY'), 'MONTH')) + 1);
DATES
2011-Feb
2011-Mar
2011-Apr
2011-May
Elapsed: 00:00:00.01
*for testing.id = 200:*
select to_char(add_months(to_date('20-JUN-2011', 'DD-MON-YYYY'), l - 1), 'YYYY-Mon') Dates
from (select level l
from dual
connect by level <= months_between(trunc(to_date('28-DEC-2011', 'DD-MON-YYYY'), 'MONTH'),
trunc(to_date('20-JUN-2011', 'DD-MON-YYYY'), 'MONTH')) + 1);
DATES
2011-Jun
2011-Jul
2011-Aug
2011-Sep
2011-Oct
2011-Nov
2011-Dec
7 rows selected.
SQL> select * from testing
2 /
ID START_DAT END_DATE
100 05-FEB-11 28-MAY-11
200 20-JUN-11 28-DEC-11
SQL> select id,
2 to_char(add_months(start_date,column_value - 1),'YYYY-Mon') dates
3 from testing,
4 table(
5 cast(
6 multiset(
7 select level
8 from dual
9 connect by add_months(trunc(start_date,'MM'),level - 1) <= end_date
10 )
11 as sys.OdciNumberList
12 )
13 )
14 order by id,
15 column_value
16 /
ID DATES
100 2011-Feb
100 2011-Mar
100 2011-Apr
100 2011-May
200 2011-Jun
200 2011-Jul
200 2011-Aug
200 2011-Sep
200 2011-Oct
200 2011-Nov
200 2011-Dec
11 rows selected.
SQL> SY.
Similar Messages
-
There two ways of finding months between two dates
ROUND((Date1 – Date2) / 365.25 * 12,2) and
ROUND(months_between(date1,date2),2) There is a slight difference between output from these two.
I think the result from the second statement should be more accurate, confirm?
AbhishekAbSHeik wrote:
There two ways of finding months between two dates
ROUND((Date1 – Date2) / 365.25 * 12,2) and
ROUND(months_between(date1,date2),2) There is a slight difference between output from these two.
I think the result from the second statement should be more accurate, confirm?
AbhishekHi Abhishek,
I also agree with this.
Coz 365.25 = (365 + (1/4))
This 0.25 actually the extra year of a Leap Year, distributed equally among 4 years. But, when the 1st formula is evaluated, the calculation might cause difference...
So as far as Oracle is concerned, MONTHS_BETWEEN should be used.
Please rectify me if i'm wrong.
Ranit B. -
How to Calculate number of months between two dates
Hi All,
In one of the fomr developments, I have to calculate the
Number of Days
Number of Months ( Considering Leap Year) provided by the dates, end user enters in the form,
After going thorugh some forum discussion, I have come to know about so many things which were not clear till now.
I have gone through various forums too, some one suggets to make use of FORM CALC and some other JAVA SCRIPT. But the logic i want to build in java script.
The most interesting point is the DATE object is not getting created when i write the below code
var startDate = new DATE(oYear, oMonth, oDay);
I am still not clear, that really the date object gets created in Adobe form If so the why the alert box is getting populated when i write below lines
var oTemp = startDate.getFullYear();
xfa.host.messagebox(oTemp);
So, there are so many unclear things,
If any one can help me by suggesting the approach and how to build the logic in the JavaScript I would be really thankful
Regards
PavanChandHi,
ChakravarthyDBA wrote:
Hi
I want number of Sundays between two dates
example
number of Sundays count between '01-04-2013' and '30-04-2013' in one select query I have to include this as sub query in my select statement.Here's one way:
SELECT early_date
, late_date
, ( TRUNC (late_date + 1, 'IW')
- TRUNC (early_date, 'IW')
) / 7 AS sundays
FROM table_x
;This does not depend on your NLS settings.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only), and also post the results you want from that data.
Point out where the statment above is getting the wrong results, and explain, using specific examples, how you get the right results from the given data in those places.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ {message:id=9360002} -
Need help to copy a photo between two events.
I would appreciate it if someone could tell me how to copy a photo I want shown in two events. I have tried to duplicate the photo and drag it as well as cut and paste the duplicate, and in both instances, both photos move. Does iPhoto not allow the same photo in two events? When I click on the photo, the copy button is not highlighted as an option. Help?
Duplicate the photo, flag one of them and go to the destination event and use the events menu ==> add flagged photo to selected event
However this is not the best way to use iPhoto - events are a very basic inflexible organizing method - having photos in albums which uses no extra space is a much better way or organizing photos
LN -
Create a table with all the months between two dates
Hi all,
I have a purchase table recording individual purchases. One of the fields is my Date field (date of purchase).
I would like to create a table 'All_months' with two entries ('month_no' and 'month') which will be based on the first and last date in the purchase table. Assuming the first purchase recorded in my purchase table was on the 12th of January 2008, the table should have the following structure:
month_no month
1 12JAN2008
2 12FEB2008
3 12MAR2008
It should continue in this fashion up-to the month where the last purchase was recorded.
I have been struggling with creating the query that would do that for days now and can't find anything when asking Mr Google.
Thanks,
ChrisWelcome to the forum!
Here's one way:
CREATE TABLE all_months
AS
SELECT LEVEL AS month_no
, ADD_MONTHS ( first_date
, LEVEL - 1
) AS month
FROM (
SELECT MIN (date_of_purchace) AS first_date
, MAX (date_of_putchase) AS last_date
FROM purchase
CONNECT BY LEVEL <= 1 + MONTHS_BETWEEN ( TRUNC (last_date, 'MONTH')
, TRUNC (first_date, 'MONTH')
;Do you really need a table like this? Every time you change the data in the purchase table, you won't know if all_months is still accurate or not. You can derive all_months in a sub-query every time you need it, or make all_months a view instead of a table.
Edited by: Frank Kulash on Jun 14, 2012 5:57 AM -
Calculation of MONTHS , YEARS , & DAYS between two dates
Hi Experts,
Currently i am using this functional Module. which calculates the number of months between two dates.
CALL FUNCTION 'FIMA_DAYS_AND_MONTHS_AND_YEARS'
EXPORTING
I_DATE_FROM = ITAB_PER-BEGDA
I_DATE_TO = ITAB_PER-ENDDA
IMPORTING
E_MONTHS = CMONTHS.
BUT this functional module Rounding off the Month...Say ..if the total numbr of days between from & to date is 105 it is showing as 4 months ...
<b>Is there any other Functional Module</b> ????which Calculates the EXACT MONTH between two dates...(in the above case the month is expected to be 3 only since it is 105 days only)..
Kindly help me regarding this....lakshimiraja,
You can use this function module.
report zrich_0003
no standard page heading.
data: duration_in_months type i.
parameters: s_date type sy-datum,
e_date type sy-datum.
call function 'DURATION_DETERMINE'
exporting
unit = 'MON'
factory_calendar = 'P6' " Your factory calendar
importing
duration = duration_in_months
changing
start_date = s_date
end_date = e_date
exceptions
factory_calendar_not_found = 1
date_out_of_calendar_range = 2
date_not_valid = 3
unit_conversion_error = 4
si_unit_missing = 5
parameters_not_valid = 6
others = 7.
write:/ duration_in_months.
Don't forget to reward if useful... -
Calculate business day between two dates
Hi Guys
I need the count of business days between two dates
Date1, Date2 i need the count only business day (exclude sartuday&sunday)
If date1 is null or nothing i need to pass 0
If date2 is null or nothing i need to pass 0
help on thisHi,
To achive this within SSRS, go to the report code window and add the below
Function getBusinessDaysCount(ByVal tFrom As Date, ByVal tTo As Date) As Integer
Dim tCount As Integer
Dim tProcessDate As Date = tFrom
For x as Integer= 1 To DateDiff(DateInterval.Day, tFrom, tTo) + 1
If Not (tProcessDate.DayOfWeek = DayOfWeek.Saturday Or tProcessDate.DayOfWeek = DayOfWeek.Sunday) Then
tCount = tCount + 1
End If
tProcessDate = DateAdd(DateInterval.Day, 1, tProcessDate)
Next
Return tCount
End Function
In the textbox where you need to display the value, add the below expression
=Code.getBusinessDaysCount(parameters!StartDate.Value,parameters!EndDate.Value)
It is assumed the you want to pass the two days from parameters named Startdate and EndDate. If not, modify the expression with required values.
Regards
Please click "Mark as Answer" if this resolves your problem or "Vote as Helpful" if you find it helpful. BH -
How to caluclate tat between two dates of one timestamp field
Hi,
could some one help how to caluclate days between two dates of single timestamp filed and with this
query
Select * from m_activity_transaction where actn_opp_id in (
Select actn_opp_id from m_activity_transaction where ACTN_ACTV_ID = 218
Group by actn_opp_id
having count(*) > 1 ) and ACTN_ACTV_ID = 218
order by actn_performed_on
iam getting output is
ACTN_ID ACTN_OPP_ID ACTN_PERFORMED_ON
319415 95831 27-JAN-12 11.06.20.000000 AM
315249 95831 08-FEB-12 05.32.54.000000 PM
301927 103509 20-DEC-11 04.01.43.000000 PM
301458 103509 19-DEC-11 04.51.03.000000 PM
294841 115840 10-JAN-12 03.20.12.000000 PM
312062 115840 11-JAN-12 05.17.06.000000 PM
and i nedd to caluclate no.of days between two dates like 27-JAN-12 11.06.20.000000 AM and 08-FEB-12 05.32.54.000000 PM where actn_id is unique AND ACTN_OPP_ID IS NOT UNIQUE.
Thanks in Advance,
vvr.This way?
with data as
select 315249 a, 95831 b, to_timestamp('27-JAN-12 11.06.20.000000 AM', 'DD-MON-RR HH.MI.SS.FF6 AM') dt from dual union all
select 319415, 95831, to_timestamp('08-FEB-12 05.32.54.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 301927, 103509 , to_timestamp('20-DEC-11 04.51.03.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 301458 , 103509 , to_timestamp('19-DEC-11 04.01.43.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 363810 , 144656 , to_timestamp('27-JUN-12 12.43.28.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 363500 , 144656 , to_timestamp('26-JUN-12 11.41.50.000000 AM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual union all
select 363354 , 144656 , to_timestamp('25-JUN-12 12.41.13.000000 PM', 'DD-MON-RR HH.MI.SS.FF6 AM') from dual
select a, b, diff
from (
select a, b, extract( day from (dt - lag(dt) over (partition by b order by dt, a)) ) diff
from data
) tab
where tab.diff is not null;
A B DIFF
319415 95831 12
301927 103509 1
363500 144656 0
363810 144656 1 -
[Urgent] Need help calculating difference between two dates
I'm trying to write a code to display the current day and time, then accept two dates from the user and display the difference between the two dates. To display the current date and time, I've used the SimpleDateFormatter library but I'm having difficulty calculating the difference between two dates. Could someone please help me with this?
Below is my code so far
import java.util.Date;
import java.util.Scanner;
import java.text.SimpleDateFormat;
public class DateFormatter {
public void displayNow()
Date todaysDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
String formattedDate = formatter.format(todaysDate);
System.out.println("Today's date and time is: "+formattedDate);
public void calculateDifference(Date startDate, Date endDate)
/* This is whre i need help! */
public static void main(String[] args)
DateFormatter df = new DateFormatter();
Scanner sc = new Scanner(System.in);
df.displayNow();
System.out.println("Please enter a date: ");
String date1 = sc.next();
System.out.println("Please enter another date: ");
String date2 = sc.next();
}The methods displayNow() and calculateDifference(Date startDate, Date endDate) are essential and cannot be skipped out.tarahmarie101 wrote:
Dude. I've tried doing assignments under pressure, and it doesn't work. Here's a piece of free advice. Start earlier next time, don't put "urgent" in the title of your post b/c it just pisses people off around here--and is a sure sign of an overdue homework assignment which no one around here likes helping with, and try to ask intelligent questions--don't just say "Help me!"
Of course, it takes some time to learn what the intelligent questions are. You have my sympathy. Try asking questions about how to do a particular task, instead of asking people to tell you what to do. It starts to get easier after a while.
actually dude i started on this on monday.. but there were so many other stuff to do along with this.. and i did try referring up the Calendar utility but it was a bit confusing actually.. it was only after that did i come to this forum.. nor am i very good at Java.. anyhoo thanks for your sympathies :P -
How to calculate the month difference between two date char. in Query?
Customers would like to see how many months passed between two date type of characteristics (e.g., the month difference between the current date and the scheduled delivery date in the record) and put the result into the column as KF.
We would have to grab the fiscal year/period kind of value and then do the subtraction, e.g., if the current date value is 2/28/2008 and the scheduled delivery date value in the record is 12/01/2007, the correct result should be 2 month difference between these two date values, but could someone here give us the technical light on how to make this happen in query design?
Thanks and we will give you reward points for the correct anwsers!Hi Kevin,
The Badi is RSR_OLAP_BADI.
You can create an implementation using Transaction SE18.
The implementation is per cube and is defined in the filters.
In the Implementation you have the following methods :
1. Define : Here you will provide the Keyfigure you need as a virtual one.
2. Initilialize : Any Init Function you want to do.
3. Compute. This is called per datarecord and here you can cimpute your value.
Hope this helps.
Pralay Ahluwalia -
List Months between a date range
Hi, I'm trying to list the months between a given date range in an ABAP report to generate a MIS report(ALV).. Say I have 2 date ranges, 01.01.2006 to 01.04.2006.. I want to list
Jan 2006
Feb 2006
Mar 2006
Apr 2006.. Any FM in SAP to get this OP..hi vivek,
use this function module to get months beween two dates
1. MONTHS_BETWEEN_TWO_DATES or
2. MONTHS_BETWEEN_TWO_DATES_NEW
please reward the point if you are satisfied with answer
thanks,
john. -
How to get list of all the dates between two dates
Hi, Can anybody please help me ..
I have two dates in string format ("dd/MM/yyyy).I need to get all the dates in between these two dates.How can I do thin in java
Thanks in advanceLook at classes Calendar and SimpleDateFormat.
And get your abstraction straight: you don't have two dates. You have two Strings that represent a date. To use them, you have to convert them to Date objects first with SDF. -
Find the difference between two dates for the specific month and year
Hi,
I have two dates, start date is 30/12/2012 and end date is 04/01/2013. Using datediff I found the difference of days between two dates. But I find the no of days in January 2013. ie output is 4 instead of 6. I input month and year to find the no of days
for that date. In this case I input Jan 2013. How can I sql this ?I don't understand how most of the answers provided here not analytically solving the problem with many cases possible.
First let me understand you:
You have 2 dates range and you want to calculate day range for specific month and year between the original date range.
declare @for_month int = 1 --January
declare @for_year int = 2013
declare @StartDate date = '2012-12-20'
declare @EndDate date = '2013-01-04'
SELECT
CASE
WHEN (DATEPART(MONTH, @StartDate) = @for_month and DATEPART(MONTH, @EndDate) = @for_month) and ((DATEPART(YEAR, @StartDate) = @for_year or DATEPART(YEAR, @EndDate) = @for_year)) THEN
DATEDIFF(DAY, @StartDate,@EndDate)
WHEN (@StartDate < cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (@EndDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, -1, @EndDate)-1, 0),@EndDate)
WHEN (@EndDate > cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) and (@StartDate between (cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) and (cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date))) THEN
DATEDIFF(DAY, @StartDate,DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, @StartDate) + 1, 0))) + 1
WHEN ((DATEDIFF(DAY, @StartDate, cast(DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date)) + 1, 0)) as date)) >= 0) and (DATEDIFF(DAY, cast(CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as date), @EndDate) >= 0)) THEN
DATEDIFF(DAY, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime), DATEADD(d, -1, DATEADD(m, DATEDIFF(m, 0, cast( CONVERT(varchar(4), @for_year) + '-' + CONVERT(varchar(2), @for_month) + '-01' as datetime)) + 1, 0))) + 1
ELSE
0
END as [DD]
I don't know how you calculate day range between 01/01/2013 and 04/01/2013
is 4, it is actually is 3 but if that is the case, you can add 1 from the condition. -
I need to implement Drag N Drop between two tables which saves both records
I need to implement Drag N Drop between two tables which saves both records in a third page, by using drag n drop.
check this video http://baigsorcl.blogspot.com/2011/01/drag-and-drop-collection-in-oracle-adf.html
-
Need help to find out link between process order and purchase order.
Hi All,
Need help to find out link between process order and purchase order.
We have purchase order, we can find out associated process order in MD09 (No Purchase Requisition found in Purchase order). When I tired to replicate this scenario with same material in system but not able to do.
Please suggest me what needs to check to get purchase order link to process order.. (this is not subcontracting )
Edited by: SAP PQ on Sep 26, 2011 5:24 PM
Thanks,
SAP PQ
Edited by: SAP PQ on Sep 26, 2011 5:24 PMMD09 is pegging. In SAP pegging is dynamic, meaning that there's no fixed link between purchase order and process order in your case.
This is why you did not get the same result when you tried again later.
Such a link can exist only if you do direct procurement for the order.
Maybe you are looking for
-
ITunes install fails due to Error Code
I'm trying to install iTunes and I get the message Service 'Apple Mobile Device' failed to start. Verify you have sufficient privileges to start system services. I then have 3 options - Abort - Retry - Ignore which provide no success. If I ignore it
-
Want to change payer and update credit limit data with out delete documents
Hi Expert, I have a problem that user feed 50-60 documents ( Sale order & delivery ) with wrong payer. Now we want to change payer and shift credit limit data ( open sale order , open delivery ) from old payer to new payer is any option for this. Tha
-
"Disk read error occurred" cannot even load System Recovery
Hello, yesterday I powered on my computer, and it was almost froze, with only the mouse moving and even the task manager not showing up when called. I first tried to make it go in standby, as I thought it was some program which didnt start as planned
-
Jndi-name in Message Driven Bean
Hi I have successfully created the ejb jar file for deployment using sun one studio . I created the message driven bean and I right clicked on the ejb and used the customize option to set the bean properties . But my problem is that the jndi-name in
-
DBMS_SPACE.OBJECT_GROWTH_TREND without Diagnostics Pack
Hi , Can i use DBMS_SPACE.OBJECT_GROWTH_TREND without Diagnostics Pack? As per the docs this procedure uses AWR for information retrieval. Also I've seen that there are multiple bugs related to this in <10.2.0.3. Can some one share their experiences