Solved 1/SQL 2k: Nothing is returning from the DB using a WHERE clause with a date datatype.

Posted on 2007-07-25
Last Modified: 2010-04-23
I have a SQL 2k Table with a col of the smallDateTime dataType (MyDateCol). From my app, I want to fill my dataTable with all the records in that table that meet a date range. The dataAdapter has two parameters:
MyDataAdapter.SelectCommand.Parameters("@StartDate").Value = New Date(2007,1,1)
MyDataAdapter.SelectCommand.Parameters("@EndDate").Value = New Date(2007,12,31)

My VB App:
MyDataAdapter.SelectCommand.CommandText = "SELECT MyCols FROM MyDataBase.dbo.MyTable WHERE (MyDateCol>= @StartDate) OR (MyDateCol<= @EndDate)"

My Sql Table
Value of MyDateCol = '2007-07-25 11:45:00'

Upon calling the fill method, nothing is returned. The above WHERE date clauses surely catch the Sql date value, but how can I get it to be recognised and thus returned?

Question by:IvanHowarth
    LVL 2

    Accepted Solution

    Try this.
    In your WHERE clause "WHERE (MyDateCol>= @StartDate) OR (MyDateCol<= @EndDate)" change it to
    WHERE (MyDateCol >= '@StartDate') OR (MyDateCol <= '@EndDate'). I handle my statements different than you but when dealing with the datetime type in SQL Server I have found that the value has to be surrounded by a single quote same as you would for a string value.

    If you are looking to return value's between the two dates then you will have to change the OR to AND or you will return all values before the enddate and all values that come after the startdate. Keep us posted.


    Author Comment

    Visual Studio (2003) didn't like the quotes. Nevertheless, it wasn't the quotes that was the problem, it was the OR. Changing it to AND and leaving out the quotes worked. Thanks.

    Before I award you the points...
    I can't get the records where  MyDateCol = '2007-07-25 11:45:00' --i.e. today when
    MyDataAdapter.SelectCommand.Parameters("@StartDate").Value = Date.Today
    MyDataAdapter.SelectCommand.Parameters("@EndDate").Value = Date.Today
    Only when I expand the range. Any ideas, or should I add a time to it e.g. 00:01 and 23:59 respectively?

    LVL 2

    Expert Comment

    I would add a time to it given that the value in your mydatecol does hold a real time. I have always used the shortdate version which defaults the time to 00:00.

    Author Comment

    Following that idea. Thanks again Dale!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

    759 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

    9 Experts available now in Live!

    Get 1:1 Help Now