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()
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.
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() .
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.
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 .
SELECT users.id, DATE_FORMAT(users.signup_date, '%Y-%m-%d') FROM users WHERE DATE(signup_date) = CURDATE()
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With