Search code examples
vb.netms-access-2016

Use Like function together with CDate function


I am writing a sql query that searches the date data type. I want to use the Like function with the CDate function, but I get a mismatch error in the data type.

Here is my code;

"SELECT * FROM Kasa_Tahsilat WHERE Tarih LIKE CDate('%" & DateTimePicker1.Value & "%') ORDER BY Kimlik DESC"

Note: I'm using VB.NET


Solution

  • You can't do that. Like is for strings, not date values, and octothorpes must be used as the delimiter:

    "SELECT * FROM Kasa_Tahsilat WHERE Tarih = #" & DateTimePicker1.Value.ToString("yyyy'/'MM'/'dd") & "# ORDER BY Kimlik DESC"
    

    For matching an interval of, say, three days:

    "SELECT * FROM Kasa_Tahsilat WHERE Tarih Between #" & DateTimePicker1.Value.AddDays(-3).ToString("yyyy'/'MM'/'dd") & "# And #" & DateTimePicker1.Value.AddDays(3).ToString("yyyy'/'MM'/'dd") & "# ORDER BY Kimlik DESC"