SQL Date format BETWEEN query

I realize you said just SQL, but it doesn’t seem like you’re getting much help, so I’m going to show you how I would go about this using MySQL and hopefully it will help:

In C#, create the query string like this:

string date1 = dt1.ToString("D");
string date2 = dt2.ToString("D");
string query = "SELECT * FROM customer WHERE cast(entered_date_time as date) > STR_TO_DATE('" + date1 + "', '%W, %M %d, %Y') AND cast(entered_date_time as date) < STR_TO_DATE('" + date2 + "', '%W, %M %d, %Y');";

To give you a better view of the query, here’s a sample what I’m doing:

SELECT * FROM customer WHERE
cast(entered_date_time as date) > STR_TO_DATE('Monday, October 01, 2012', '%W, %M %d, %Y')
AND
cast(entered_date_time as date) < STR_TO_DATE('Tuesday, October 02, 2012', '%W, %M %d, %Y')

Also notice I used the field entered_date_time since date is a reserved keyword in MySQL.

Browse More Popular Posts

Leave a Comment