Results 1 to 8 of 8
  1. #1
    sergi117 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2018
    Posts
    30

    iif datediff Select Query


    I am setting up for a select query, a column that if the difference between two dates is exactly (down to day) 6 years apart or more then return a 1 if not then 0. I've set up an iif(datediff("y",date1column,date2column)>=2191,1, 0) but I'd like for it to be completely accurate as I know due to slight differences, leap year etc it doesn't always lead to 6 years=2191. Would anyone have a suggestion so I could have this query have more certainty in being 6 years or more apart? Thanks
    Last edited by sergi117; 09-12-2018 at 05:48 PM.

  2. #2
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Here is one way:
    Code:
    IIf((Month([date1column])=Month([date2column])) And (Day([date1column])=Day([date2column])) And (Year([date2column])-Year([date1column])=6),1,0)

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 7 32bit Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Sorry. Realised original suggestion will not work
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  4. #4
    sergi117 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2018
    Posts
    30
    Apologies as I should have clarified, I meant in a situation of 6 or more years apart, but considering down to the day in difference. Your idea is great, but again by my own fault, I need the formula to not necessarily ignore dates that are past 6 years in difference.

  5. #5
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I meant in a situation of 6 or more years apart, but considering down to the day in difference. Your idea is great, but again by my own fault, I need the formula to not necessarily ignore dates that are past 6 years in difference.
    Simple modification to my formula. Just change the "=6" to ">=6", i.e.
    Code:
    IIf((Month([date1column])=Month([date2column])) And (Day([date1column])=Day([date2column])) And (Year([date2column])-Year([date1column])>=6),1,0)

  6. #6
    JSR is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2018
    Location
    Reno, NV
    Posts
    41
    Quote Originally Posted by JoeM View Post
    Simple modification to my formula. Just change the "=6" to ">=6", i.e.
    Code:
    IIf((Month([date1column])=Month([date2column])) And (Day([date1column])=Day([date2column])) And (Year([date2column])-Year([date1column])>=6),1,0)
    That will only return dates that are exactly 6, 7, ... years apart, and nothing in between, such as 6 years and 1 day.

    You want:

    iif(year([date2])-year([date1])>6 or (year([date2])-year([date1])=6 and month([date2])>month([date1])) or (year([date2])-year([date1])=6 and month([date2])=month([date1]) and day([date2])>=day([date1])),1,0)

  7. #7
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    That will only return dates that are exactly 6, 7, ... years apart, and nothing in between, such as 6 years and 1 day.
    I think I may have misunderstood the request...
    I thought that only wanted difference that were exact years apart. But in re-reading it, I think you may be correct.

  8. #8
    sergi117 is offline Advanced Beginner
    Windows 10 Access 2013 32bit
    Join Date
    Sep 2018
    Posts
    30
    Thanks everybody!

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

Similar Threads

  1. datediff function in query
    By LeesKeys in forum Queries
    Replies: 3
    Last Post: 06-23-2016, 08:54 AM
  2. DateDiff Value in a Query
    By jjz0512 in forum Programming
    Replies: 13
    Last Post: 05-06-2015, 12:28 PM
  3. datediff in query
    By xeon_tsd in forum Programming
    Replies: 6
    Last Post: 04-08-2014, 01:40 AM
  4. DateDiff in a query
    By Jim.H. in forum Access
    Replies: 4
    Last Post: 01-22-2012, 01:45 PM
  5. Query datediff error
    By RayMilhon in forum Queries
    Replies: 3
    Last Post: 10-05-2011, 12:13 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