I want to return all records that were added to the database within the last 30 days. I need to convert the date to mm/dd/yy because of display purposes.
create_date between DATE_FORMAT(curdate(),'%m/%d/%Y') AND (DATE_FORMAT(curdate() - interval 30 day,'%m/%d/%Y'))
My statement fails to limit the records to the last 30 days - it selects all the records.
Can anyone point me in the right direction? It feels like I am close.
Thanks and have a great week.
You can also write this in mysql -
You need to apply
DATE_FORMAT
in theSELECT
clause, not theWHERE
clause:Also note that
CURDATE()
returns only theDATE
portion of the date, so if you storecreate_date
as aDATETIME
with the time portion filled, this query will not select the today's records.In this case, you'll need to use
NOW
instead:DATE_FORMAT
returns a string, so you're using two strings in yourBETWEEN
clause, which isn't going to work as you expect.Instead, convert the date to your format in the
SELECT
and do theBETWEEN
for the actual dates. For example,For the current date activity and complete activity for previous 30 days use this, since the SYSDATE is variable in a day the previous 30th day will not have the whole data for that day.
Here's a solution without using
curdate()
function, this is a solution for those who useTSQL
I guess