34

I have a quick question. I'm have a db an audit Table with a datetime column in it. (i.e 2012-03-27 00:00:00) and I'm building a mySQL query to return a set of rows if the date is in between the two dates I'm giving it.

so far my query looks like this:

SELECT * FROM util_audit WHERE DATED >= DATE(03/15/2012) AND DATED <= DATE(03/31/2012);

if I just use

SELECT * FROM util_audit WHERE DATED >= DATE(03/15/2012); 

It'll return all my record because they were dated this week.

I also tried this:

SELECT * FROM util_audit WHERE DATED >= '02/15/2012 00:00:00' AND DATED <= '03/31/2012 00:00:00';

and nothing! It'll return zero rows, when I know I have all of them dated from the 27 of this month to today. Am I missing something here? why does it work on its own, but not when I add the second date?I'm probably overlooking something.

3 Answers 3

56

Try:

SELECT * FROM util_audit WHERE `DATED` BETWEEN "2012-03-15" AND "2012-03-31";
Sign up to request clarification or add additional context in comments.

4 Comments

What happens when you have more search criteria (eg. SELECT * FROM util_audit WHERE DATED BETWEEN "2012-03-15" AND "2012-03-31" AND booLive = 1)? Because I get errors.
I can't reproduce that error, but in any case you can always use parentheses for clarity, e.g. SELECT * FROM util_audit WHERE (DATED BETWEEN "2012-03-15" AND "2012-03-31") AND booLive = 1;
To get ALL the data for a specific month/year, you can always start the day at 00 and go to 31 (no matter what the month). This will also capture dates where the day portion is unknown, such as 2015-02-00. So for example: SELECT * FROM records WHERE start_date BETWEEN "2015-02-00" AND "2015-02-31";
You can no longer use the 31st of the month for months that don't have 31 days in newer versions of MySQL. This must have changed sometime between v5 and v8. It now gives an error "Incorrect DATETIME value '2020-04-31'"
16

There's a few edge cases which need to be caught here by using the correct end date, if not items on the 31st are ignored:

SELECT * FROM util_audit 
WHERE DATED >= '2012-02-15 00:00:00' AND DATED <= '2012-03-31 23:59:59';

SELECT * FROM util_audit 
WHERE DATED BETWEEN '2012-02-15 00:00:00' AND '2012-03-31 23:59:59';

Or you could push the end date forward and use:

SELECT * FROM util_audit 
WHERE DATED >= '2012-02-15 00:00:00' AND DATED < '2012-04-01';

SELECT * FROM util_audit 
WHERE DATED BETWEEN '2012-02-15 00:00:00' AND '2012-04-01';

Just came across this myself so hope it helps if other people find this page.

1 Comment

I accidentally down voted this question. This is the date format I used! So did an edit, reversed my vote, and deleted the edit. Sorry for that.
7

As far as I know, dates in MySql are represented with the format yyyy-MM-dd hh:mm:ss hence you need to do this:

SELECT * FROM util_audit 
WHERE DATED >= '2012-02-15 00:00:00' AND DATED <= '2012-03-31 00:00:00';

Or even better:

SELECT * FROM util_audit 
WHERE DATED BETWEEN '2012-02-15 00:00:00' AND '2012-03-31 00:00:00';

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.