I'm creating a SQL server stored procedure that is doing a lot of filtering based on the current week. Rather than passing in a start and end date, I am passing in a single date and using Datepart() to filter out the week.
Where DATEPART(WEEK, cd.TurninDate) = DATEPART(WEEK, @StartDate)
and DATEPART(YEAR, cd.TurninDate) = DATEPART(YEAR, @StartDate)
Trying top be proactive, I can see managers asking for this to be run Monthly or Quarterly as well as Weekly. I'd rather not have a stored Proc for each version if I can help it. I want to pass in the frequency and the date but it doesn't seem to work.
I tried:
Where DATEPART(@Freq, cd.TurninDate) = DATEPART(@Freq, @StartDate)
and DATEPART(YEAR, cd.TurninDate) = DATEPART(YEAR, @StartDate)
but I get an error on the @Freq not being recognized as an interval.
Does anyone know a way I can get around that?