Ady Năstase
Ady Năstase

Reputation: 259

C# database compare one Date/Time field from a table with today

To make the story short I have a table inside of which I have a column of type Date/Time. I used MS Acces 2013 to create the database. Now, I need at a certain point in my app to check and delete all the records that are having their date smaller than today. Let's say that conn is my connection to my database. I wrote:

conn.Open(); 
string delRec = "DELETE FROM myTable WHERE myDateTimeColumn < '" + DateTime.Now + "'";
ExecQuery(delRec);
conn.Close();

If I replace the string with, let's say:

string delRec = "DELETE FROM myTable WHERE anIntColumn < 21";

everything is running just fine. What am I doing wrong? Many thanks.

Upvotes: 1

Views: 54

Answers (3)

Pushpendra Singh
Pushpendra Singh

Reputation: 46

Have you tried DateTime.Now.Day method i.e.

conn.Open(); string delRec = "DELETE FROM myTable WHERE myDateTimeColumn < '" + DateTime.Now.Day + "'"; ExecQuery(delRec); conn.Close();

Hope it will help you.

Upvotes: 0

Jitendra.Suthar
Jitendra.Suthar

Reputation: 110

ACCESS is not recognizing as a date passed in WHERE clause.

conn.Open();
string delRec = "DELETE FROM myTable WHERE myDateTimeColumn < '#" + DateTime.Now + "#'";
ExecQuery(delRec);
conn.Close();

There may be one more point to check that are you passing same date format in where clause parameter. e.g 'YYYY/MM/DD'

Can you try this one. Hope this will help

Upvotes: 0

Simon
Simon

Reputation: 1081

You could use the built in Now() function:

string delRec = "DELETE FROM myTable WHERE myDateTimeColumn < Now()";

Upvotes: 1

Related Questions