Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Select Date Equal to Today

Tags:

mysql

I'm trying to run a mysql select statement where it looks at today's date and only returns results that signed up on that current day. I've currently tried the following, but it doesn't seem to work.

SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d')      FROM users      WHERE users.signup_date = CURDATE() 

I've modified my SELECT statement to this, thanks guys.

SELECT id FROM users WHERE DATE(signup_date) = CURDATE()

like image 408
Jako Avatar asked Oct 01 '12 16:10

Jako


People also ask

Where date is today MySQL?

We can get the today's date in MySQL using the built-in date function CURDATE(). This function returns the date in 'YYYYMMDD' or 'YYYY-MM-DD' format depending on whether a string or numeric is used in the function. The CURRENT_DATE and CURRENT_DATE() both are the synonyms of the CURDATE() function.

How can I get current date data?

Simply use the CURDATE() function to get the current date. The date can be displayed in two different formats: ' YYYY-MM-DD ' if it is used in a string context or YYYYMMDD if it is used in a numeric context. There are two other functions that can be used instead of CURDATE() : CURRENT_DATE and CURRENT_DATE() .

How can I compare current date and date in MySQL database?

MySQL has the ability to compare two different dates written as a string expression. When you need to compare dates between a date column and an arbitrary date, you can use the DATE() function to extract the date part from your column and compare it with a string that represents your desired date.

How do I get today's date in SQL?

To get the current date and time in SQL Server, use the GETDATE() function. This function returns a datetime data type; in other words, it contains both the date and the time, e.g. 2019-08-20 10:22:34 .


2 Answers

SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d')  FROM users  WHERE DATE(signup_date) = CURDATE() 
like image 139
Barmar Avatar answered Oct 06 '22 01:10

Barmar


This query will use index if you have it for signup_date field

SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d')      FROM users      WHERE signup_date >= CURDATE() && signup_date < (CURDATE() + INTERVAL 1 DAY) 
like image 35
Sergii Stotskyi Avatar answered Oct 05 '22 23:10

Sergii Stotskyi