Comparing two date fields in a SQL query

Posted on 2001-08-01
Medium Priority
Last Modified: 2012-06-27

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.


Question by:syntel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 43

Expert Comment

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

Author Comment

ID: 6341102

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)

Author Comment

ID: 6341150
Just tried that. VB reports 'variable not defined'
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Accepted Solution

dgorin earned 800 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

Author Comment

ID: 6342176

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.



Expert Comment

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".
LVL 53

Expert Comment

by:Ryan Chong
ID: 6342958
Hi syntel,

N = 0

Select Count(Date1-Date2 > N) From Table


Author Comment

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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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 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…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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