Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to check if date is between two month-day values

Tags:

date

sql

mysql

I want to know if a date (Year-Month-Day) is between a day of the month (Month-Day).

For example, I want to know if 'April 2, 2013' is between 'April 2' and 'April 19'.

I can easily do this of the range has a year value. Howver, without a year, I need ideas how to do this.

Some examples of what I'm trying to achieve:

SELECT 1 WHERE '2013-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-03-02' BETWEEN '04-01' AND '04-19'; -- result is 1

Thanks

like image 830
llt Avatar asked Oct 16 '25 13:10

llt


1 Answers

You can make use of DATE_FORMAT().

Example

SELECT 
*
FROM
 table
WHERE
  DATE_FORMAT(date,'%m-%d') BETWEEN '04-01' AND '04-19'

Edit

SELECT 
   *
FROM
   table
WHERE
   STR_TO_DATE(date, '%m-%d') BETWEEN '04-01' AND '04-19'
like image 106
Dipesh Parmar Avatar answered Oct 18 '25 06:10

Dipesh Parmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!