Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query to get records from same day

Tags:

date

sql

mysql

I wish to find all records of the current day. I have a field Date of type DATE.

If I use

WHERE `Date` = '2011-04-07'

it works

but if I use:

WHERE  `Date`='CURDATE()'

or

WHERE  `Date`='NOW()'

it does not return any results (when there actually are some).

How do I get the current date in the right format to use it in my SQL query?

I am using MySQL And the date was originally entered in the database using NOW().

like image 208
Timothée HENRY Avatar asked Jan 21 '23 01:01

Timothée HENRY


1 Answers

Use

WHERE `Date`=CURDATE()

The quotes (') are used to wrap up a string (text).

Edit: I can see now that you say the value was stored with NOW() : it probably includes a time element too. Will update answer imminently..

This will compare the date part of the Date field to today's date:

WHERE DATE(`Date`)=CURDATE()
like image 57
Kieren Johnstone Avatar answered Jan 29 '23 14:01

Kieren Johnstone