Mysql KURDATE with month

Asked

Viewed 20 times

1

In Mysql if I want to search only dates that are in the current year, I do so:

WHERE dia = year(CURDATE())

But I’ve been trying to find dates that are in the same month as the current one:

WHERE dia = month(CURDATE())

and does not accept, returns empty, even having dates in the current month registered, how to do?

1 answer

1

You need the function MONTH on both sides. The same goes for function YEAR that you’re using.

SELECT
  *
FROM
  tabela
WHERE
  MONTH(tabela.data) = MONTH(CURRENT_DATE()) AND
  YEAR(tabela.data) = YEAR(CURRENT_DATE())

Remembering that using the month filter without the year filter, you will end up with records from previous years that are from the month of May, for example. Combine the month filter with the year filter as above.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.