Solved

Comparing two date fields in a SQL query

Posted on 2001-08-01
8
329 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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 50

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 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