sql server - Stored Procedure Help: Passing a Year as a parameter to get results -


i'm trying input year (not entire date) set of results tell me amount of things sold year. i'm not sure how go doing however. below example of have down.

alter proc orderyear (@orderyear datetime)  select orderdate [year],count(salesorderid) count sales.salesorderheader @orderyear=year(orderdate) group orderdate  begin set nocount on end  exec orderyear '2005' 

you want express query predicate range, because range sargable:

select orderdate [year],count(salesorderid) count sales.salesorderheader orderdate >= @firstdayofyear , orderdate <= @lastdayofyear group orderdate; 

so trick computer @firstdayofyear , @lastdayofyear given year. i'll leave exercise reader.


Comments