0

I have tried

SQLCommand = "SELECT * 
              FROM myTable 
              WHERE recDateTime 
                    BETWEEN '30/12/2013 23:49:09' AND '31/12/2013 00:01:29'"

But it returns a "Data type mismatch in criteria expression." OleDb Exception. Am I doing anything wrong here? Because I can't seem to see the problem.

recDateTime is a DateTime field. And those two dates are DateTimes

I am using OLEDB in VB.NET.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Thahleel al-Aleem
  • 711
  • 2
  • 10
  • 20

3 Answers3

0
SQLCommand = "SELECT * 
              FROM myTable 
              WHERE  format(recDateTime,'dd/MM/yyyy') 
                     BETWEEN '30/12/2013' AND '31/12/2013'"

Try this Code

SELECT * 
FROM   wwwh 
WHERE  Format(entry_date, 'MM/dd/yyyy hh:NN:ss tt') BETWEEN 
       '12/10/2013 09:00:00 AM' AND '12/11/2013 10:00:00 AM' 
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36
0

There is a fair chance that the database is trying to interpret the literal date strings in en-US format. You can eliminate that problem entirely by using parameters in the query, like this:

Dim oledbSql = "SELECT * FROM myTable WHERE recDateTime BETWEEN ? AND ?"
Dim oledbSqlCmd As New OleDbCommand(oledbSql, oledbConn)
Dim dtStart As New DateTime(2013, 12, 30, 23, 49, 9)
Dim dtEnd As New DateTime(2013, 12, 31, 0, 1, 29)

oledbSqlCmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@Start", .OleDbType = OleDbType.Date, .Value = dtStart})
oledbSqlCmd.Parameters.Add(New OleDbParameter With {.ParameterName = "@End", .OleDbType = OleDbType.Date, .Value = dtEnd})
' now execute the query

You don't actually have to name the parameters, but it makes it easier to see what is happening (which aids maintainability). You do, however, have to add them in the order in which they are used as a ? is used as a placeholder for each parameter.

If you are using SQL Server as the database, it is better (because it is optimised for it) to use System.Data.SqlClient rather than System.Data.OleDb, and you can then use named parameters in the query:

Dim sql = "SELECT * FROM myTable WHERE recDateTime BETWEEN @Start AND @End"
Dim sqlCmd As New SqlCommand(sql, sqlConn)
Dim dtStart As New DateTime(2013, 12, 30, 23, 49, 9)
Dim dtEnd As New DateTime(2013, 12, 31, 0, 1, 29)

sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@Start", .SqlDbType = SqlDbType.DateTime, .Value = dtStart})
sqlCmd.Parameters.Add(New SqlParameter With {.ParameterName = "@End", .SqlDbType = SqlDbType.DateTime, .Value = dtEnd})
' now execute the query

Please don't ever be tempted to use .AddWithValue as it will make assumptions about the data type which will foul things up unexpectedly.

Also, you should (almost) always explicitly state the columns you want from the database instead of using * because the database is free to return the columns in any order it wants to - which may not be the order you think they will be returned in.

Depending on the actual database you are using, parameter names might need to have something like a : instead of an @ as a prefix.

[I thought I might as well answer the question rather than only be negative about other proposed answers.]

Community
  • 1
  • 1
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
-3
 Dim da As OleDbDataAdapter = New OleDbDataAdapter(
            "SELECT * 
            FROM TableID
            WHERE (Format(ColumnID, 'hh:NN:ss tt') 
                  BETWEEN '00:00:00 AM' AND '01:00:00 AM') And [ColumnID] = True ",
  "Provider = Microsoft.Jet.OLEDB.4.0;" & "Data Source =Database1.mdb")
Chris Fung
  • 79
  • 1
  • 4