Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mySQL dateTime range Query Issue

Tags:

datetime

mysql

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.

like image 469
Myy Avatar asked Mar 30 '12 00:03

Myy


People also ask

How do I select a date range in MySQL?

If you need to select rows from a MySQL database' table in a date range, you need to use a command like this: SELECT * FROM table WHERE date_column >= '2014-01-01' AND date_column <= '2015-01-01'; Of course you need to change: table.

How do I pass a date range in SQL query?

SELECT * FROM PERSONAL WHERE BIRTH_DATE_TIME BETWEEN '2000-01-01 00:00:00' AND '2002-09-18 12:00:00';

How do I query between two dates using MySQL?

select *from yourTableName where yourColumnName between 'yourStartingDate' and curdate().

What is the format of datetime in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.


1 Answers

Try:

SELECT * FROM util_audit WHERE `DATED` BETWEEN "2012-03-15" AND "2012-03-31"; 
like image 87
Adi Gandra Avatar answered Sep 20 '22 02:09

Adi Gandra