One way:
Build a small table of month names and month numbers
Create Table Months (MonthName varchar (10) Not Null Primary Key,
MonthNumber char(2) Not Null)
Insert Months (MonthName, MonthNumber)
Select 'January', '01'
Union All Select 'February', '02'
Union All Select 'March', '03'
-- etc
Union All Select 'December', '12';
Then you can use this table to build datetimes with the start date as the
first date of the month from and the end date as the first date of the month
following the month to. Then use these dates to select from your table.
For example,
Declare @Year int,
@MonthFrom varchar(10),
@MonthTo varchar(10),
@StartDate datetime,
@EndDate datetime;
Select @Year = 2006, @MonthFrom = 'January', @MonthTo = 'December';
Select @StartDate = Cast(@Year as char(4)) + '/' + m.MonthNumber + '/01'
From Months m
Where m.MonthName = @MonthFrom;
Select @EndDate = DateAdd(mm, 1, Cast(@Year as char(4)) + '/' +
m.MonthNumber + '/01')
From Months m
Where m.MonthName = @MonthTo;
-- Now do the select from your table
SELECT product_number,
delivery_date,
....
from dbo.archive
Where trandate >= @StartDate
AND traxdate < @EndDate
AND product_number = 1234
group by
product_number,
delivery_date
order by product_number;
Tom
[quoted text, click to view] "Chris" <Chris@discussions.microsoft.com> wrote in message
news:92E7371A-7509-4472-81CB-B8BF6D032865@microsoft.com...
> The issue I have is, the application (asp.net) is passing the following
> params
>
> year = 2006
>
> month from = January
>
> month to = July
>
> and they expect me to look this up as between 2006-01-01 and 2006-07-31.
> How
> then should I accomplish this?
>
> Thanks
>
>
> "Andrew J. Kelly" wrote:
>
>> Well first off you should never format a WHERE clause like that, meaning
>> as
>> soon as you add functions to the column you loose the ability to do an
>> index
>> seek. You should always change what you are comparing the column to so
>> you
>> can do a direct compare to the values in the column.
>>
>> where traxdate BETWEEN '20060101' AND '20060228'
>> AND product_number = 1234
>>
>> Second how do you expect "datename(month,traxdate) between 'January' AND
>> 'February'" to really work? DateName returns a varchar. How are you
>> expecting it to do a BETWEEN amongst all the months? Keep the values as
>> Datetimes and you should be fine.
>>
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "Chris" <Chris@discussions.microsoft.com> wrote in message
>> news:DD4F9BC8-B0B9-4DB9-BFE5-4C5C0ED1EB99@microsoft.com...
>> > Hi,
>> > I am having problems with this query (sample)
>> >
>> >
>> > SELECT product_number,
>> > delivery_date,
>> >
>> > ...
>> >
>> > from dbo.archive
>> >
>> > where year(year,traxdate) = '2006'
>> > AND datename(month,traxdate) between 'January' AND 'February'
>> > AND product_number = 1234
>> >
>> > group by
>> > product_number,
>> > delivery_date
>> > order by product_number
>> >
>> > it doesn't returns all the values. it seems to skip those months with
>> > less
>> > than 31 days.
>> >
>> > I need the date like this since the app passing in the parameters have
>> > three
>> > options
>> >
>> > year
>> > month from
>> > month to
>> >
>> > I even tries using a calendar table but still the same issue.
>> >
>> > Please assist,
>> >
>> > Thanks
>> >
>>
>>
>>