SQL Server : get records where dates are in specific time fram -


let's have folling table

eventname    |   eventstartdate  |   eventenddate -------------------------------------------------- event 1      |  11/11/2015       |   01/31/2016 event 2      |  01/24/2016       |   01/26/2016 event 3      |  02/23/2015       |   03/20/2016 event 4      |  02/20/2016       |   02/26/2016 

i'd write query gets events event takes place within calendar month. example want events active in january. leave me event 1, event 2 , event 3

any appreciated

this can done using bunch of conditions like:

select *  your_table      (eventstartdate >= '20160101' , eventstartdate <= '20160131')      or      (eventenddate >= '20160101' , eventenddate <= '20160131')      or      (eventstartdate <= '20160101' , eventenddate >= '20160131') 

first 1 defines period starts in jan.

second 1 defines perion ends in jan.

third 1 defines period starts before , ends after jan (inclusively).

obviously - there no other periods intersecting particular january possible.

update:

all these conditions simplified to:

select *  your_table eventstartdate <= '20160131' , eventenddate >= '20160101' 

this condition still defines 3 periods mentioned above, shorter.


Comments