473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DateDiff function question

Hello all. I have a really basic question that I hope someone has a
better answer for. I apologize in advance-- I know this is probably a
really basic question but I am used to Oracle rathern than Access.

I have a database where they customer wants to purge records from
certain tables after three years. When I was asked to make the changes
I originally thought this was an incredibly simple thing to do. I
looked at the function list and assumed that a one line query using
DateDiff was the answer.

Unfortunately, the DateDiff function with the "yyyy" does not actually
calculate the number of years difference between two dates; it returns
the number of times the year portion of the date changes. For example,

DateDiff("yyyy",#31/12/2004#, #24/06/2005) returns 1
DateDiff("yyyy",#01/01/2005#, #24/06/2005) returns 0

even though the time difference is not a full year between the two
dates.

I know I can do days instead but then I have to add the calculations
for leap years, etc. and it just doesn't seem like a nice clean and
elegant way to do it. Is there a function or something that I am
missing?

Thanks.
Bill

Nov 13 '05 #1
5 6655
Let's try a different function instead.

Example:
DateAdd("yyyy", -3, Date())

You could also use DateSerial which splits the year, month, and date into
separate items.

Example:
DateSerial(Year(Date()) - 3, Month(Date()), Day(Date()))

You would then use the result of either of these in the criteria.

Example Criteria for the date field:
<=DateAdd("yyyy", -3, Date())

This will filter the query to only return records whose date is 3 years ago
or older.

--
Wayne Morgan
MS Access MVP
<mc******@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hello all. I have a really basic question that I hope someone has a
better answer for. I apologize in advance-- I know this is probably a
really basic question but I am used to Oracle rathern than Access.

I have a database where they customer wants to purge records from
certain tables after three years. When I was asked to make the changes
I originally thought this was an incredibly simple thing to do. I
looked at the function list and assumed that a one line query using
DateDiff was the answer.

Unfortunately, the DateDiff function with the "yyyy" does not actually
calculate the number of years difference between two dates; it returns
the number of times the year portion of the date changes. For example,

DateDiff("yyyy",#31/12/2004#, #24/06/2005) returns 1
DateDiff("yyyy",#01/01/2005#, #24/06/2005) returns 0

even though the time difference is not a full year between the two
dates.

I know I can do days instead but then I have to add the calculations
for leap years, etc. and it just doesn't seem like a nice clean and
elegant way to do it. Is there a function or something that I am
missing?

Thanks.
Bill

Nov 13 '05 #2
Doh!
Boy do I feel stupid. :-)

Thanks. That works great.

Bill

Nov 13 '05 #3
mc******@yahoo.com wrote:
Hello all. I have a really basic question that I hope someone has a
better answer for. I apologize in advance-- I know this is probably a
really basic question but I am used to Oracle rathern than Access.

I have a database where they customer wants to purge records from
certain tables after three years. When I was asked to make the changes
I originally thought this was an incredibly simple thing to do. I
looked at the function list and assumed that a one line query using
DateDiff was the answer.

Unfortunately, the DateDiff function with the "yyyy" does not actually
calculate the number of years difference between two dates; it returns
the number of times the year portion of the date changes. For example,

DateDiff("yyyy",#31/12/2004#, #24/06/2005) returns 1
DateDiff("yyyy",#01/01/2005#, #24/06/2005) returns 0

even though the time difference is not a full year between the two
dates.

I know I can do days instead but then I have to add the calculations
for leap years, etc. and it just doesn't seem like a nice clean and
elegant way to do it. Is there a function or something that I am
missing?

Thanks.
Bill


This seems related to determining how old someone is given their
birthdate.

If you don't mind using the Format() function in a SQL string,

SELECT Int(Format([CDate],"yyyy.mmdd") - Format([BDate],"yyyy.mmdd"))
AS Age FROM tblBirthdays;

Originally I used Val() around the Format but I don't think I need it.
If you don't mind assuming True = -1,

SELECT Year([CDate]) - Year([BDate]) + IIf(Month([BDate]) =
Month([CDate]), Day([BDate]) > Day([CDate]), Month([BDate]) >
Month([CDate])) AS Age FROM tblBirthdays;

else

SELECT Year([CDate]) - Year([BDate]) + IIf(Month([BDate]) =
Month([CDate]), IIf(Day([BDate]) > Day([CDate]), -1, 0),
IIf(Month([BDate]) > Month([CDate]), -1, 0)) AS Age FROM tblBirthdays;

should be quite robust.

Note that the solution on Dev's site,

SELECT DateDiff("yyyy",[Bdate],[CDate]) + Int(Format([CDate],"mmdd") <
Format([Bdate],"mmdd")) AS Age FROM tblBirthdays;

only requires "mdd" rather than "mmdd" in both spots. BDate in
tblBirthdays stands for BirthDate and CDate stands for CurrentDate. I
envisioned something like Date() to be used in place of [CDate]. Be
sure to test any of these methods before use including tests like
3/1/03 to 2/29/04 that mess up simpler solutions that use the day of
the year. Perhaps some unforseen condition that messes up my solution
caused someone to split the calculation into two parts.

James A. Fortune

Nov 13 '05 #4
<ji********@compumarc.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
mc******@yahoo.com wrote:
Note that the solution on Dev's site,

SELECT DateDiff("yyyy",[Bdate],[CDate]) + Int(Format([CDate],"mmdd") <
Format([Bdate],"mmdd")) AS Age FROM tblBirthdays;

only requires "mdd" rather than "mmdd" in both spots.


Not so. You need the preceding 0 so that, for instance, February birthdays
are known to occur in October.

Format(#2/14/1990#, "mdd") will yield 214, while Format(#10/1/2005#, "mdd")
will yield 1001. Since we're comparing text values here, 214 is NOT less
than 1001. 0214 is less than 1001 though.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

Nov 13 '05 #5

Douglas J. Steele wrote:
Format(#2/14/1990#, "mdd") will yield 214, while Format(#10/1/2005#, "mdd")
will yield 1001. Since we're comparing text values here, 214 is NOT less
than 1001. 0214 is less than 1001 though.

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)


You're not comparing text values there. Try it out. SQL reads 0214 as
214. Maybe you're thinking of Format$.

James A. Fortune

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: News Central | last post by:
To all! I use the DateDiff function using VB6 and get this error 'Wrong number of argument or invalid property assignment' ... have anyone seen this problem? thanks ....
7
by: Drago | last post by:
Hi, I got the next question, I want to know the diference between two dates and thats is really easy, but my problem is get that diference in the following format ex. "the diference is= 0 year,...
1
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
3
by: chanchito_cojones | last post by:
i have a question regarding the DateDiff function. I am quite new to access and seem to have hit a snag with this function. My problem is as follows: I have a table field that list a persons...
6
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
14
by: cmdolcet69 | last post by:
I'm trying to use the DateDiff function to calculate the difference whether a shift has been setup. when i run the code below with strFirstShiftEnd as a stringor date or datetime. I get an error...
2
by: muddasirmunir | last post by:
i am using vb 6 , i had place two datepicker in form now i want to calcuate differcen of month in two date for this i used the function datediff i had try it withh many syntax but getting error...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.