I have a Mysql Table that is used for a log file on the that table there is a field called 'log_date' And it stores the date in the following format( %Y-%m-%d %H:%i.%s ).On the DB the dates look like something this 2013-20-05 00:00.00. Lets say today's date is 2013-20-05 And I have log files from 2013-01-01 to present day. If I run a query like this:
SELECT * FROM log_table
WHERE STR_TO_DATE(log_date, '%Y-%m-%d %H:%i.%s') < '2013-05-05 00:00.00'
This is returning every row in the DB including rows that are greater than 2013-05-05 00:00.00
And if I reverse the < (less then) to a > (greater then) with a query that looks like this:
SELECT * FROM log_table
WHERE STR_TO_DATE(log_date, '%Y-%m-%d %H:%i.%s') > '2013-05-05 00:00.00'
Then it returns ZERO rows. I think the time stamp is what is causing the problem I have worked with the date format before but not the DateTime format. Why is this happening?
log_date should be of DateTime data type. It is much simpler to use MySQL DATE function. Some examples
SELECT * FROM log_table
WHERE DATE(log_date) < '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) > '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) BETWEEN '2013-04-05' AND '2013-05-05'
SELECT * FROM log_table
WHERE DATE(log_date) BETWEEN DATE(CURRENT_DATE() - INTERVAL 2 WEEK) AND
DATE(CURRENT_DATE() + INTERVAL 4 DAY)