Solved

Comparing two date fields in a SQL query

Posted on 2001-08-01
8
332 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 

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 51

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

730 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