22

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?

1
  • 1
    What is the data type for log_date?
    – Kermit
    May 20, 2013 at 19:53

2 Answers 2

46

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)
4
  • It may be simpler but it's not efficient. I'll +1 you if you tell me why.
    – Kermit
    May 20, 2013 at 21:04
  • 2
    okay, ill shoot :). impossible to use indexes with built-in functions in the WHERE clause
    – blotto
    May 20, 2013 at 21:07
  • Thank you blotto I have tried using just the DATE function but still no effect. I think it has something to do with the format of the date itself "2013-05-05 00:00.00" with it including the %H:%i.%s'. the strange part is the less then sign will return all rows regardless of the date and the greater then always returns zero rows. In the past when I only use the YYYY-DD-MM it sorts perfectly but this format includes time.
    – Ryan Bauer
    May 20, 2013 at 23:08
  • 1
    Thank you everybody the problem was one of syntax I was inserting a period in the last part of my date 2out the b013-20-05 00:00 . 0 (without the blank space) when on the DB the format used : on the last digit. the final code looked like this: SELECT * FROM WHERE DATE_FORMAT(log_date , '%Y-%d-%m %H:%i:%s') > '2013-18-05 00:00:00'
    – Ryan Bauer
    May 21, 2013 at 2:03
1

You can try with that..

WHERE DATE_FORMAT(AUCTION_DATE, '%Y%m%d') >= DATE_FORMAT('2013/5/18', '%Y%m%d')

You can also get today date using now() function.

1
  • 2
    You shouldn't do that: the DATE_FORMAT() is computed for every row, instead of testing against a constant, which DATE_FORMAT('2013/5/18', '%Y%m%d') is. So you'd better: WHERE AUCTION_DATE >= DATE_FORMAT('2013/5/18', '%Y%m%d'), which can use indexes.
    – Yvan
    Mar 10, 2015 at 5:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.