There are various functions that you can use to achieve this, such as DATEPART and DATETIFF . However, the real problem is not how to express the state of StartDate or EndDate, which falls on a given month, but how to do it in such a way as to make the request effective. In other words, how to express it with SARGable.
If you are looking for a small change table, something under 10k pages, then that doesn’t make such a big difference, a full scan is probably quite acceptable. The real question is that the table (s) are significant in size and a full scan is unacceptable.
If you do not have an index in any StartDate or EndDate column, it does not matter, the criteria are not searchable, and the query will scan the entire table anyway. However, if there are indexes on StartDate and EndDate, then how you express this condition matters. The critical part of DATETIME indices is that you must express the search as an exact date range. Expressing the condition as a function depending on the DATETIME field will make the condition incurable, which will lead to a full table scan. Thus, this knowledge makes the right way to search for a date range:
select ... from table where StartDate between '20091101' and '20091201' or EndDate between '20091101' and '20091201';
It can also be expressed as:
select ... from table where StartDate between '20091101' and '20091201' union all select ... from table where EndDate between '20091101' and '20091201' and StartDate not between '20091101' and '20091201';
Which query works best depends on several factors, such as the size of the table and the statistics of the actual data in the table.
However, you need the month of November from any year that this request does not give you. The solution to this problem contradicts any instinct that a programmer has: hard code of the corresponding years. In most cases, the tables have a small set of years in any case, something in the range of 4-5 years of past data and plan for 3-4 years more until the system is revised:
select ... from table where StartDate between '20051101' and '20051201' or EndDate between '20051101' and '20051201' union all select ... from table where StartDate between '20061101' and '20061201' or EndDate between '20061101' and '20061201' union all ... select ... from table where StartDate between '20151101' and '20151201' or EndDate between '20151101' and '20151201';
There are 12 months of the year, write 12 separate procedures. Does that sound crazy? That's for sure, but it's the best thing from a SQL query compiler and optimizer. How can such code be supported? 12 separate procedures, with a query that is repeated 10 times (20 times, if you use UNION between StartDate and EndDate to remove OR), 120 repetitions of the code, it should be insensitive. This is actually not the case. Use code generation to create procedures such as XML / XSLT so you can easily modify and save it. Should the client know about 12 procedures and call the appropriate one? Of course not, it calls one wrapper procedure that distinguishes the @Month argument to invoke the correct one.
I believe that anyone who looks at the system after the facts most likely believes that this request was written by a group of drunken monkeys. However, somewhere between parameterization, SARGability indexing, and SQL DATETIME, the fact that this is a modern state when it refers to search calendar intervals arises.
Oh, and if the request falls into the Index Tipping Point , it will still disable the entire argument ...
Update
By the way, there is a cheap way out if you are willing to sacrifice some memory space: two constant calculated columns on StartMonth AS DATEPART(month, StartDate) and EndDate AS DATEPART(month, EndDate) , and the index on each and the query WHERE StartMonth = @Month OR EndMonth = @Month (or again UNION between the two asks for one for “Start one for the end” to remove the OR).