×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Number of Months in Date Range (Not Between)

Number of Months in Date Range (Not Between)

Number of Months in Date Range (Not Between)

(OP)
This returns the count of 3 months (which is correct):
SELECT round(MONTHS_BETWEEN (DATE '2012-12-07', DATE '2012-10-12') + 1) FROM dual;
Dec
Nov
Oct

However I am trying to return the count of 2 for the following:
SELECT round(MONTHS_BETWEEN (DATE '2013-01-07', DATE '2012-12-31') + 1) FROM dual;

This returns 1 which is incorrect - should return 2 as there is Jan and Dec...

RE: Number of Months in Date Range (Not Between)

Something like "Select count (distinct (reformat datefield as yyyy+mm)) from data source.

====================================
Sometimes the grass is greener on the other side because there is more manure there - original.

RE: Number of Months in Date Range (Not Between)

(OP)
Added TRUNC and seems to do the trick...

SELECT MONTHS_BETWEEN (TRUNC(DATE '2012-12-07','MONTH'), TRUNC(DATE '2012-10-12','MONTH')) + 1 FROM DUAL;
SELECT MONTHS_BETWEEN (TRUNC(DATE '2013-01-07','MONTH'), TRUNC(DATE '2012-12-31','MONTH')) + 1 FROM DUAL;

RE: Number of Months in Date Range (Not Between)

Jason,

(See my post in thread759-1708993: Count number of months in Date range, as well.)

Let's go back to some fundamentals of DATE arithmetic in Oracle. Using your date values, above:

CODE

SELECT DATE '2012-12-07' - DATE '2012-10-12' Diff_in_Days FROM DUAL;

DIFF_IN_DAYS
------------
          56

select DATE '2013-01-07' - DATE '2012-12-31' Diff_in_Days FROM DUAL;

DIFF_IN_DAYS
------------
           7 

Now, if we evaluate the MONTHS_BETWEEN these two sets of dates (without "+1" and without ROUND or TRUNC), we get:

CODE

SELECT MONTHS_BETWEEN (DATE '2012-12-07', DATE '2012-10-12') Diff_in_Months FROM DUAL;

DIFF_IN_MONTHS
--------------
    1.83870968

SELECT MONTHS_BETWEEN (DATE '2013-01-07', DATE '2012-12-31') Diff_in_Months FROM DUAL;

DIFF_IN_MONTHS
--------------
    .225806452 

Now, reconciling the two sets of code, we see that "7 days are .225806452 of a month" and "56 days are 1.83870968 months". If you TRUNC, ROUND, or add a month into the mix, it certainly affects the results. But Oracle is doing exactly what you prescribed it to do in your code.

Did this clear things up, or did it muddy the waters?




santaMufasa
(aka Dave of Sandy, Utah, USA)
“People may forget what you say, but they will never forget how you made them feel."

RE: Number of Months in Date Range (Not Between)

(OP)
Issue resolved using TRUNC

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close