Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


VB.Net - Compare SQL DateField Ignoring Time

Posted on 2014-02-17
Medium Priority
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,
Question by:Jimbo99999
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 18

Accepted Solution

ChetOS82 earned 1000 total points
ID: 39865692
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.

Author Comment

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.


Author Comment

ID: 39866341

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

LVL 28

Assisted Solution

Ark earned 1000 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)
End Using

Open in new window


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 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