VB.NET Sql statement with datetime filter

Posted on 2006-04-07
Last Modified: 2010-08-05

I want to select some rows from a SQL server database using vb.NET. The selection is based on a datetime field in Vb.NET (the datetime should be between two values which are colums in my sql table). I can't seem to find the correct format.

The statement should be something like

Select * from Table Where dotNetDate Between DateColumn1 and DateColumn2

If tried this in but it returns nothing...

sqlCmd = New SqlCommand("SELECT * FROM Table WHERE '" + pdatDate.ToString("d/MM/yyyy hh:mm:ss") + "' between FromDateTime and ToDateTime", objSQLConn)

Tnx for the help.
Question by:tsay
    LVL 8

    Expert Comment

    Hi tsay,

    try to compare it to each column: where pdatDate.ToString("d/MM/yyyy hh:mm:ss") > fromdatetime and pdatDate.ToString("d/MM/yyyy hh:mm:ss") < todatetime

    LVL 142

    Accepted Solution

    this is the proper way to avoid all the problems:

    sqlCmd = New SqlCommand("SELECT * FROM Table WHERE @d between FromDateTime and ToDateTime", objSQLConn)
    sqlCmd.Parameters.Add("@d", pdatDate )
    LVL 3

    Expert Comment

    try this, vb seems to have this touch and go feelin when using the "+"

    dim strdate as string =pdatDate.ToString("d/MM/yyyy hh:mm:ss")
    sqlCmd = New SqlCommand("SELECT * FROM Table WHERE '" & strdate & "' between FromDateTime and ToDateTime", objSQLConn)
    LVL 5

    Author Comment

    Tnx angelIII.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    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 this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 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

    24 Experts available now in Live!

    Get 1:1 Help Now