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 ?
...
AND DATE_FORMAT(t.DELIV_DATE, '%m%d') BETWEEN '0101' AND '0201'
...
Update - to handle range that loops though the end year (replace 0101 and 0201 with actual variables representing from and to):
...
AND
  (DATE_FORMAT(t.DELIV_DATE, '%m%d') BETWEEN '0101' AND '0201'
   OR '0101' > '0201' AND
     (DATE_FORMAT(t.DELIV_DATE, '%m%d') >= '0101' OR
      DATE_FORMAT(t.DELIV_DATE, '%m%d') <= '0201'
     )
  )
...
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With