I have a table with birthdays:
CREATE TABLE birthdays
(`name` varchar(10), `day` datetime)
;
INSERT INTO birthdays
(`name`, `day`)
VALUES
('Joe', '2000-03-18'),
('Mike', '2000-12-23'),
('Albert', '2000-02-15');
I want to find all person with birthday inside a given interval. It works fine, when my two dates are in the same year:
SELECT *
FROM birthdays
WHERE DATE_FORMAT(day, "%m-%d") BETWEEN DATE_FORMAT('2021-12-01',"%m-%d") AND DATE_FORMAT('2021-12-31',"%m-%d");
But the moment I filter between two dates over year end, I get nothing. I tried two approaches: DATE_FORMAR and DAYOFYEAR, with the same result:
SELECT *
FROM birthdays
WHERE DATE_FORMAT(day, "%m-%d") BETWEEN DATE_FORMAT('2021-11-28',"%m-%d") AND DATE_FORMAT('2022-01-01',"%m-%d");
SELECT *
FROM birthdays
WHERE DAYOFYEAR(day) BETWEEN DAYOFYEAR('2021-11-28') AND DAYOFYEAR('2022-01-01')
Here is a SQL Fiddle with the problem.