Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date between dates, ignore year

What's the best (fastest) approach to compare if date is in range of dates independently from year?

table "dates":

    some_column| since        | upto        |  
    -----------|--------------|-------------|
   'foo'       | '2011-05-01' | '2013-06-01'|

Now I want this query to return 'foo'

SELECT foo FROM dates WHERE '2014-05-05' BETWEEN `since` AND `upto`

If needed, I can change type/format of stored dates in "dates" table, but I cannot change format of date which I put into query as that value is typically from another table (It's part of more complex query using joins).

like image 256
gadelat Avatar asked Sep 01 '14 00:09

gadelat


1 Answers

Use the DayOfYear function:

 SELECT foo FROM dates WHERE DayOfYear('2014-05-05') 
    BETWEEN DayOfYear(`since`) AND DayOfYear(`upto`)
like image 199
Larry Lustig Avatar answered Oct 13 '22 01:10

Larry Lustig