Solved

Comparing two date fields in a SQL query

Posted on 2001-08-01
8
327 Views
Last Modified: 2012-06-27
Hi,

I'm using ADO in VB with an Access database.  I need to compare two date fields in a query to see how much time elapsed between the two dates.

In other parts of the program I am using the DateAdd function to do this.  In the query however I need something that I can use in SQL that will serve to provide the same functionality as DateAdd except in this case inside the query comparing whatever values are in the fields.

Thanks,

Jim
0
Comment
Question by:syntel
8 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6341066
dateadd is part of the vba function library so it should work in the same way within the query.
0
 

Author Comment

by:syntel
ID: 6341102
TimCottee:

Are you sure that it will work with a *field name* which is NOT a variable name and serves a completely different purpose??

DateAdd("n", -SomeTime, FieldNameHere)
0
 

Author Comment

by:syntel
ID: 6341150
Just tried that. VB reports 'variable not defined'
0
 
LVL 5

Accepted Solution

by:
dgorin earned 200 total points
ID: 6341269
This will return the difference between two date/time fields as a calculated column.  In this case, the difference is returned as the number of minutes, refer to the DATEDIFF documentation for other ways to use this function.

This may or may not be portable to all databases but will work fine with ADO to Access or SQL Server.

select StartDate, EndDate, DATEDIFF(n, StartDate, EndDate) as theDiff from ALERTS
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:syntel
ID: 6342176
dgorin:

Thanks for the tip.  I'm still trying to figure out how to use this function in the WHERE clause of a query.  I want the query to return records depending on how much time has elapsed since the date in field1 vs. field2.  So far I haven't found anything using the DATEDIFF in the WHERE part of the query and sadly I don't have my books here with me either.


Thanks,

Jim
0
 
LVL 2

Expert Comment

by:BozzoCage
ID: 6342256
select *  from my_table  where (datefield2-datefield1) >10;

in this case it will return all records with date difference of more than 10 days. Didn't checked in help on how to define what category (minutes, months,...) to use for comparison, just used some "testing".
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 6342958
Hi syntel,

N = 0

Select Count(Date1-Date2 > N) From Table

?
0
 

Author Comment

by:syntel
ID: 7106350
Sorry, didn't realize I left this open.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now