Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 570
  • Last Modified:

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.
0
tsay
Asked:
tsay
1 Solution
 
Jupiler78Commented:
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!
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this is the proper way to avoid all the problems:
http://www.knowdotnet.com/articles/defaultparameters.html
http://www.knowdotnet.com/articles/dynamicsqlparameters.html

sqlCmd = New SqlCommand("SELECT * FROM Table WHERE @d between FromDateTime and ToDateTime", objSQLConn)
sqlCmd.Parameters.Add("@d", pdatDate )
0
 
mazkotCommented:
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)
0
 
tsayAuthor Commented:
Tnx angelIII.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now