Solved

SQL Query to find date between two datetime fields

Posted on 2013-12-09
6
517 Views
Last Modified: 2013-12-09
I have a webform with a gridview. The gridview loads based on a date that equals the visitDate field or the endVisitDate field or a date between the two.

I have the part that selects the date equal to the visitDate or equal to the endVisitDate but I'm getting stuck on how to pull the date from between the two.

This is the query that works:
"SELECT visitorKey, visitorName1, visitorName2, visitDate, visitEndDate
FROM dbo.VisitorLog WHERE (DATEDIFF(d, visitDate, GETDATE()) = 0) OR (DATEDIFF(d, visitEndDate, GETDATE()) = 0) "

How do I do the third part selecting between the two dates?
Any help would be appreciated
0
Comment
Question by:lakhi
6 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39706679
What you are asking is slightly confusing. You have a web form with two fields that will load a gridview. So you want to filter the data in your VisitorLog table based on these fields. So you can easily do this by using the BETWEEN keyword:

WHERE visitDate BETWEEN <visit start date field> AND <visit end date field>

If you need the end date to be inclusive, use the DATEADD field to add 1 day to it (or handle in code prior to passing it to your query).
0
 

Author Comment

by:lakhi
ID: 39706743
I am very sorry. I left part of my issue out. The gridview loads only the names of the visitors who will be in today. So the visitDate could be today, the visitEndDate could be today or today could be part of the range of dates between the visitDate and the visitEndDate.
People can submit requests for a visit on one day only or submit an advance request for a visit of several days.

So the problem, as I see it, is to find "today" between the visitDate and the visitEndDate.

Could I still use "between" to cover all of three possibilities?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39706747
In general it's better to avoid "between" for dates.  The below should work for you:

      getdate() < visitendDate and
      getdate() >= visitDate
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 39706804
SQL expert PortletPaul wrote an entire article on Beware of Between that would be a good read.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 300 total points
ID: 39707004
SELECT visitorKey, visitorName1, visitorName2, visitDate, visitEndDate
FROM dbo.VisitorLog
WHERE
    GETDATE() >= visitDate AND
    DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) <= visitEndDate
0
 

Author Closing Comment

by:lakhi
ID: 39707171
The solution worked perfectly. It was just what I needed to know. Thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

863 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

26 Experts available now in Live!

Get 1:1 Help Now