I am working on a program that takes takes a start date and an end date and returns separate values for Years, Months, and Days. Each category has its own section of code, this is what I have been using and so far its been semi-accurate until days (and sometimes months.I am not even trying to fool with leap-year at this point)
Ex Start: 04/10/2000 End: 04/10/2006 should give me 6 years 0 months and 0 days.
Years Code:
SELECT
trunc(months_between((to_date(:main_DT_DateEnd1,'MM/DD/YYYY')),(to_date(:main_DT_DateBeg1,'MM/DD/YYYY'))) / 12) as "Years1"
FROM dual
Months Code:
SELECT
trunc(mod(months_between((to_date(:main_DT_DateEnd1,'MM/DD/YYYY')),(to_date(:main_DT_DateBeg1,'MM/DD/YYYY'))), 12)) as "Months1"
FROM dual
Days Code: I have tried multiple versions of these without much success for example I can calculate total days between days but since there are different months in certain days dividing becomes more of a hassle. This is the closest one I am getting where if the days are the same then no calculation is needed, else subtract them using a substring. 1)
SELECT
CASE
WHEN substr((to_date(:main_DT_DateBeg1,'MM/DD/YYYY')),4,5) = substr((to_date(:main_DT_DateEnd1,'MM/DD/YYYY')),4,5)
THEN 0
WHEN substr((to_date(:main_DT_DateBeg1,'MM/DD/YYYY')),4,5) < substr((to_date(:main_DT_DateEnd1,'MM/DD/YYYY')),4,5)
THEN to_number(substr((to_date(:main_DT_DateEnd1,'MM/DD/YYYY')),4,5)) - to_number(substr((to_date(:main_DT_DateBeg1,'MM/DD/YYYY')),4,5))
END as "Days_1"
FROM dual
Thanks for your time, for those of you wondering this is for a job experience calculator :)