MONTHS_BETWEEN function clarification [message #582398] |
Thu, 18 April 2013 02:12  |
 |
mmi78
Messages: 31 Registered: April 2013 Location: dhaka
|
Member |
|
|
Need clarification about months_between function ..
C:\>sqlplus/nolog
SQL*Plus: Release 10.2.0.3.0 - Production on Thu Apr 18 13:20:43 2013
Copyright (c) 1982, 2006, Oracle. All Rights Reserved.
SQL> conn scott@orcl
Enter password:
Connected.
SQL> select months_between('28-feb-2013','28-jan-2013') from dual;
MONTHS_BETWEEN('28-FEB-2013','28-JAN-2013')
-------------------------------------------
1
SQL>
SQL> select months_between('28-feb-2013','29-jan-2013') from dual;
MONTHS_BETWEEN('28-FEB-2013','29-JAN-2013')
-------------------------------------------
.967741935
SQL>
SQL> select months_between('28-feb-2013','30-jan-2013') from dual;
MONTHS_BETWEEN('28-FEB-2013','30-JAN-2013')
-------------------------------------------
.935483871
SQL>
SQL> select months_between('28-feb-2013','31-jan-2013') from dual;
MONTHS_BETWEEN('28-FEB-2013','31-JAN-2013')
-------------------------------------------
1
SQL>
when date is 29 or 30 jan, then result is less than 1, but when date is 28 or 31 jan then result is 1, why? if 28 is 1 then why 29 or 30 is less than 1?
[EDITED by LF: fixed topic title typo; was "add_months"]
[Updated on: Thu, 18 April 2013 02:44] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: add_months function clarification [message #582417 is a reply to message #582416] |
Thu, 18 April 2013 04:45   |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
You quoted me but you didn't read me.
Forget any example, give specification that does not refer to any example.
Specify first, give an example after.
Otherwise I will give you something that works for 28 feb and you will tell me "yes but it does not for this date", I'll give you a solution for this latter and you wxill tell me...
regards
Michel
[Updated on: Thu, 18 April 2013 04:46] Report message to a moderator
|
|
|
|
Re: add_months function clarification [message #582426 is a reply to message #582422] |
Thu, 18 April 2013 05:25   |
 |
Michel Cadot
Messages: 68765 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
If DD2=last day and DD1>=DD2 then ceil(months_between) else months_between:
SQL> def refdat=to_date('28/02/2013','DD/MM/YYYY')
SQL> with
2 dates as (
3 select to_date('25/01/2013','DD/MM/YYYY')+level dat
4 from dual connect by level <= 7
5 )
6 select dat, months_between(&refdat, dat) diff,
7 case
8 when &refdat = last_day(&refdat)
9 and extract(day from dat) >= extract(day from &refdat)
10 then ceil(months_between(&refdat, dat))
11 else months_between(&refdat, dat)
12 end new_diff
13 from dates
14 order by 1
15 /
DAT DIFF NEW_DIFF
------------------- ---------- ----------
26/01/2013 00:00:00 1.06451613 1.06451613
27/01/2013 00:00:00 1.03225806 1.03225806
28/01/2013 00:00:00 1 1
29/01/2013 00:00:00 .967741935 1
30/01/2013 00:00:00 .935483871 1
31/01/2013 00:00:00 1 1
01/02/2013 00:00:00 .870967742 .870967742
Regards
Michel
[Updated on: Thu, 18 April 2013 05:53] Report message to a moderator
|
|
|
|