You really should store dates in columns of type DATE
. This would make your life a lot simpler.
Fortunately, MySQL has built-in functions to help you work with dates that are stored as text. MONTH(next_due)
won't work, because MySQL doesn't know how to parse next_due
as a date value. You'll have to tell it how first: MONTH( STR_TO_DATE(next_due, '%d-%b-%Y') )
.
The STR_TO_DATE
function will parse a string into a date, given a format specifier of how to parse the string. That %d-%b-%Y
format specifier should match your format if your days are two digits. If you store dates as 5-Nov-2020
(with a single digit for the day) use %e
instead of %d
. The full list of options for your format are listed with the DATE_FORMAT
function in the documentation.
So the following query should work for your purposes:
SELECT * FROM new_regis WHERE MONTH(STR_TO_DATE(next_due, '%d-%b-%Y')) = MONTH(CURRENT_DATE())
AND YEAR(STR_TO_DATE(next_due, '%d-%b-%Y')) = YEAR(CURRENT_DATE())