0

This query was working before, but maybe I changed it somehow..

select * from completed_games where date BETWEEN 2015-02-22 00:00:00 AND 2015-02-28 00:00:00

Why would this query not work, and how can I properly write it?

Error:

ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '00:00:00 AND 2015-02-28 00:00:00' at line 1
5
  • 1
    Please note that it's better to explicitly use comparison operators for a date range (rather than 'between',) since it's easier to know whether it includes the date or not by looking for an equal sign. Commented Feb 23, 2015 at 15:09
  • 1
    @mbomb007 Easier for who? Commented Feb 23, 2015 at 15:38
  • @Strawberry See here, between is not recommended. stackoverflow.com/a/749663/2415524 Commented Feb 23, 2015 at 21:25
  • @mbomb007 I think that's a spurious argument. As one of the commentator's suggests, it's like pointing out that 10.2 is not between 5 and 10. And the example provided is nonsensical. I don't understand why its attracted so much rep. Commented Feb 23, 2015 at 21:43
  • It's definitely a readability issue. Why not be clear and use standard comparison operators. Also, funny that you couldn't even come up with your own counter-argument. Commented Feb 23, 2015 at 21:49

3 Answers 3

2

Your dates are not valid. You need to enclose them between quotes.

select *
from completed_games
where date BETWEEN '2015-02-22 00:00:00' AND '2015-02-28 00:00:00'
Sign up to request clarification or add additional context in comments.

Comments

1

You need to put quotes around your date strings:

select * 
from completed_games 
where date BETWEEN '2015-02-22 00:00:00' 
    AND '2015-02-28 00:00:00'

Comments

1

If you don't need to check a time you can even do the following:

SELECT * 
FROM completed_games 
WHERE `date` BETWEEN '2015-02-22' AND '2015-02-28'

Tip: Try to use backticks (`) around keywords or special chars.

3 Comments

This is, in my opinion, the best answer.
I think this a question about MySQL.
MySQL uses backticks instead of square brackets.

Your Answer

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