Skip to content Skip to sidebar Skip to footer

MySql Query- Date Range Within A Date Range

I use mySql 5 and IIS. I have products, that have a start date field and an end date field. I need to run a query that will take user entered Start and End dates, and output the nu

Solution 1:

If your products have a start_date and an end_date and your query has a qstart_date and a qend_date, then we want the number of days between:

GREATEST(start_date, qstart_date)

and

LEAST(end_date,qend_date)

. In MySQL I think this looks like

1 + DATEDIFF ( 'd' , GREATEST(start_date, qstart_date) , LEAST(end_date,qend_date) ) 

And you'll want to ignore negative numbers, replacing them with "0".


Post a Comment for "MySql Query- Date Range Within A Date Range"