You could take a (syntactically corrected ;) ) version of what @jcarbillon suggested. As mentioned before, you have to be careful about the international date formats.
If you don't want to do that, then @adam_k's solution won't work if your selected dates cross over a year boundary. You need to have a WAY more complicated where clause to cover boundary conditions.
Here's some sample code covering scenarios where you either have start/end dates, or start month/year and end month/year:
declare @start_date datetime
declare @end_date datetime
declare @start_year int
declare @start_month int
declare @end_year int
declare @end_month int
-- Just populating variables for boundary testing...use your own if you like
select @start_date = dateadd(m, 3, getdate()), @end_date = dateadd(m, 20, getdate())
select @start_year = year(@start_date), @start_month = month(@start_date)
select @end_year = year(@end_date), @end_month = month(@end_date)
-- Version using dates
select * from dbo.myDateTestingTable
where -- cases where date is start year, and month is greater than start month
( year(myTestDate) = year(@start_date)
and month(myTestDate) >= month(@start_date)
)
or -- cases where date is past start year, but not in end year
( year(myTestDate) > year(@start_date)
and year(myTestDate) < year(@end_date)
)
or -- cases where date is end year, but less than end month
( year(myTestDate) = year(@end_Date)
and month(myTestDate) <= month(@end_date)
)
-- Version using separate month and year parameters
select * from dbo.myDateTestingTable
where -- cases where date is start year, and month is greater than start month
( year(myTestDate) = @start_year
and month(myTestDate) >= @start_month
)
or -- cases where date is past start year, but not in end year
( year(myTestDate) > @start_year
and year(myTestDate) < @end_year
)
or -- cases where date is end year, but less than end month
( year(myTestDate) = @end_year
and month(myTestDate) <= @end_month
)
I know it's ugly, but it works. And it saves you from having to assemble dates and thereby avoids the whole international date format thing.
Oh, and here's some code to build a test scenario to prove it yourself:
create table dbo.myDateTestingTable
(
myId int identity(1, 1) not null,
myTestDate datetime not null
)
declare @myDate datetime
declare @i int
set @i = 0
select @myDate = getdate()
while @i < 100 -- just a little mini-randomizing for test dates.
begin
select @myDate = dateadd(d, @i, dateadd(m, 1, @myDate))
insert into dbo.myDateTestingTable
(myTestDate)
values
(@myDate)
set @i = @i + 1
end
select * from dbo.myDateTestingTable
Sorry for the length of the post. Hope all this helps! Good luck!