Solved

VB.Net - Compare SQL DateField Ignoring Time

Posted on 2014-02-17
5
403 Views
Last Modified: 2014-03-04
Good Day Experts!

I have a bit of an issue that I am trying to work through with regards to a DateTime field in my SQL table.  It contains the time after the date.  

I give the User a Start Date and and End Date to enter on the UI.  I want to retrieve records in the SQL table based on a date field.  So, I am "asking" in the SQL query for the date field to be >= StartDate and <= End Date.  

How do I get the query to look at the date field without respect to the time on the back of the date?

Thanks for helping,
jimbo99999
0
Comment
Question by:Jimbo99999
5 Comments
 
LVL 18

Accepted Solution

by:
ChetOS82 earned 250 total points
ID: 39865692
CONVERT (DATE, DATE_FIELD)  >= StartDate AND CONVERT(DATE, DATE_FIELD) <= EndDate
0
 
LVL 40
ID: 39865911
And maybe, the first thing to do, if you do not need them, would be not to store the time in the database.

Too many programmers use Date.Now when recording the current date, which always saves the time along the date and ends up in most situations creating problems such as yours.

Date.Today is the expression to use when you do not need the time.
0
 

Author Comment

by:Jimbo99999
ID: 39866339
Hey James:

Unfortunately I am working with data that is already in the tables.  But, I understand your sentiments with not saving the time.

Thanks,
jimbo99999
0
 

Author Comment

by:Jimbo99999
ID: 39866341
Chet:

Thanks for responding. I will try it out in the morning when I get to work.

Thanks,
jimbo99999
0
 
LVL 27

Assisted Solution

by:Ark
Ark earned 250 total points
ID: 39888175
Why not using query with parameters?
 'Assuming startDate/endDate were entered from UI
startDate = startDate.Date 'get date part only
endDate = endDate.AddDays(1).Date 'get date part for next day
Dim dt As New DataTable
Using da As New SqlDataAdapter("SELECT * FROM YourTable WHERE StartDate>=@StartDate AND EndDate<@EndDate", yourConnectionString)
    da.SelectCommand.Parameters.AddWithValue("@StartDate", startDate)
    da.SelectCommand.Parameters.AddWithValue("@EndDate", endDate)
    da.Fill(dt)
End Using

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

15 Experts available now in Live!

Get 1:1 Help Now