Normally if I want to make a query on a table by date range I’ll do it this way:
SELECT DISTINCT c.ID AS 'id' FROM CUST c
JOIN TICKET t ON s.ID = t.SALE_ID
WHERE c.ACTIVE_IND = 1
AND t.DELIV_DATE BETWEEN '01-01-2012' AND '01-02-2012'
ORDER BY t.DELIV_DATE DESC
Now I need to make the same query but ignore the year, so I can say from February 28 to March 2 and year doesn’t matter.
I tried modifying the query:
SELECT DISTINCT c.ID AS 'id' FROM CUST c
JOIN TICKET t ON s.ID = t.SALE_ID
WHERE c.ACTIVE_IND = 1
AND MONTH(t.DELIV_DATE) BETWEEN ... AND ...
AND DAY(t.DELIV_DATE) ... BETWEEN ...
ORDER BY t.DELIV_DATE DESC
Above query works fine if the starting DAY is smaller than the ending. that means if I go from lets say Feb 20 to Feb 28 it works fine but if I go with Feb 28 to Mar 2 it won’t work.
Any solution for this that I can make this happen in a single query ?
Update – to handle range that loops though the end year (replace
0101and0201with actual variables representingfromandto):