0

I have a database where it is stored the date of creation of the record. The problem is that when I try to SELECT all records within 1 year, I get no rows returned.

I created a SQLFiddle to illustrate what I have and what I'm trying to do: http://sqlfiddle.com/#!9/33a32b/8

SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
  FROM aux
  WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
  ORDER BY `formatted_date` DESC;
1
  • You should put your code also here. SQLFiddle is not always online. Commented Mar 23, 2017 at 1:02

1 Answer 1

2

This is your query:

SELECT aux.description, DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS `formatted_date`
FROM aux
WHERE aux.date_creation BETWEEN 'CURDATE() - INTERVAL 1 YEAR' AND 'CURDATE()'
ORDER BY `formatted_date` DESC;

The single quotes are incorrect. You have a string constant, not a date expression. Presumably, date_creation is in the past, so all you need is:

SELECT aux.description,
       DATE_FORMAT(aux.date_creation, '%d/%m/%Y às %H:%i') AS formatted_date
FROM aux
WHERE aux.date_creation >= CURDATE() - INTERVAL 1 YEAR
ORDER BY aux.date_creation DESC;

Note that I also changed the ORDER BY. Normally, one wants Jan 2nd to follow Jan 1st, not Feb 1st.

Sign up to request clarification or add additional context in comments.

1 Comment

I was using the functions within the single quotes and was getting no problem. That just happened with this single query. Weird stuff. Thanks a lot, man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.