Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT * FROM datetime = CURDATE() return an empty result set

Can anyone tell me where's my mistake? By using that query, it should return some rows where the data have datetime = '2012-10-12' right? Here is my reference

My datetime column = 'YYYY-MM-DD HH:MM:SS', data type = datetime.

I am using XAMPP v1.8.0, MySQL v5.5.25a.

like image 866
yudayyy Avatar asked Oct 12 '12 03:10

yudayyy


People also ask

What does Curdate () do in SQL?

Definition and Usage. The CURDATE() function returns the current date. Note: The date is returned as "YYYY-MM-DD" (string) or as YYYYMMDD (numeric). Note: This function equals the CURRENT_DATE() function.

What is the difference between Curdate and now in MySQL?

The NOW() function gives current datetime as a timestamp while CURDATE() gives only current date, not time.

How can I get only date from timestamp in SQL query?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.

What is the SQL query to display current date?

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.


1 Answers

Try CASTing datetime to date by using DATE()

SELECT * 
FROM tableName
WHERE DATE(`datetime`) = DATE(CURDATE())

YYYY-MM-DD HH:MM:SS is not equal to YYYY-MM-DD

2012-01-01 12:12:12 is not equal to 2012-01-01 00:00:00
like image 177
John Woo Avatar answered Sep 30 '22 04:09

John Woo