Solved

Comparing two date fields in a SQL query

Posted on 2001-08-01
8
326 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
Comment Utility
dateadd is part of the vba function library so it should work in the same way within the query.
0
 

Author Comment

by:syntel
Comment Utility
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
Comment Utility
Just tried that. VB reports 'variable not defined'
0
 
LVL 5

Accepted Solution

by:
dgorin earned 200 total points
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:syntel
Comment Utility
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
Comment Utility
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
Comment Utility
Hi syntel,

N = 0

Select Count(Date1-Date2 > N) From Table

?
0
 

Author Comment

by:syntel
Comment Utility
Sorry, didn't realize I left this open.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now