Link to home
Start Free TrialLog in
Avatar of tsay
tsay

asked on

VB.NET Sql statement with datetime filter

Hey,

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 vb.net 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.
Avatar of Jupiler78
Jupiler78
Flag of Belgium image

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

Cheers!
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mazkot
mazkot

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)
Avatar of tsay

ASKER

Tnx angelIII.