Results 1 to 3 of 3
  1. #1
    TonyB is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Oxfordshire, UK
    Posts
    16

    Adding months to years in queries

    Hi,



    I will try and explain as best I can!

    I'm writing a query, where I have calculated the number of months between a start date and and end date:

    No of Months: Round(([EndDate]-[Startdate])/30.42)

    This works fine, but I want to Access to return the result as "X years, and X months". So for example, if the start date is 01/01/2011 and end date is 31/12/2011, I want Access to return a result of "3 years and 0 months", rather than just "36 months". Similarly, if the start date is 01/08/2009 and the end date is 31/01/2010 at the moment it tells me 6 months, and I want it to read as 0 years and 6 months.

    I've used:

    No of Years: Round(([EndDate]-[Startdate])/365.25)

    which is fine, but obviously this will round months to the nearest year (so 6 months becomes 1 year).

    I tried using the DateDiff function, but found it to be pretty useless for this scenario, as it doesn't round anything (so 01/01/2011 - 31/12/2013 is calculated as 2 years, rather than 3!)

    Grateful for any suggestions

    Thanks

  2. #2
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    This might get you in the right direction:

    http://support.microsoft.com/kb/290190

    Alan

  3. #3
    jzwp11 is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2010
    Location
    Dayton, OH
    Posts
    2,901
    You have to use the month interval in the datediff() function not the year interval

    Something like this perhaps

    SELECT INT(DateDiff("m",startdate,enddate)/12) & " years " & DateDiff("m",startdate,enddate)-(int(DateDiff("m",startdate,enddate)/12)*12) & " months " AS ElapsedTime
    FROM myTable;

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 06-09-2011, 09:15 AM
  2. Select changes in the years
    By acs_one in forum Queries
    Replies: 8
    Last Post: 11-27-2010, 05:26 PM
  3. Replies: 1
    Last Post: 11-12-2010, 01:16 AM
  4. Adding data to tables using queries
    By HunterEngineeringCoop in forum Queries
    Replies: 3
    Last Post: 10-28-2010, 12:42 PM
  5. Replies: 2
    Last Post: 04-04-2010, 06:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums