I have sp that takes for example today's date and run it through some variables with finds other dates based upon today's date. Everything is off my 1 week and I am trying to figure out how to solve this problem.
Below is current code.
Date is passed to this parameter
@dteRequest = '10/04/2007'
This part of the code is causing the problem:
SET @dteRequest_Start = DATEADD(dd, -(DATEPART(ww, @dteRequest) * 7) + 1, @dteRequest)
I have tired to change how hes looking for 01/01/2007 but then throughs everything all out of wack. So I am asking for some advise on which code changes would solve this problem.
CODE:
--Set week starting day as monday
SET DATEFIRST 1
SET @dteRequest_Start = DATEADD(dd, -(DATEPART(ww, @dteRequest) * 7) + 1, @dteRequest)
SET @dteRequest_End = DATEADD(dd, 1, @dteRequest)
--Get the start/End date for the prior year
SET @dteCompare_End = DATEADD(yy, -1, @dteRequest_End)
--If day is not sunday then add the appropriate number of days to it to get sunday
IF DATEPART(dw, @dteCompare_End) <> 7
BEGIN
SET @dteCompare_End = DATEADD(dd, 7 - DATEPART(dw, @dteCompare_End), @dteCompare_End)
END
--If week is not same week of year as Start then add or subtract week as appropriate
IF DATEPART(ww, DATEADD(dd, -1, @dteRequest_End)) <> DATEPART(ww, @dteCompare_End)
BEGIN
SET @dteCompare_End = DATEADD(wk, DATEPART(ww, DATEADD(dd, -1, @dteRequest_End)) - DATEPART(ww, @dteCompare_End), @dteCompare_End)
END
SET @dteCompare_Start = DATEADD(dd, -(DATEPART(ww, @dteCompare_End) * 7) + 1, @dteCompare_End)
SET @dteCompare_End = DATEADD(dd, 1, @dteCompare_End)
Below is current code.
Date is passed to this parameter
@dteRequest = '10/04/2007'
This part of the code is causing the problem:
SET @dteRequest_Start = DATEADD(dd, -(DATEPART(ww, @dteRequest) * 7) + 1, @dteRequest)
I have tired to change how hes looking for 01/01/2007 but then throughs everything all out of wack. So I am asking for some advise on which code changes would solve this problem.
CODE:
--Set week starting day as monday
SET DATEFIRST 1
SET @dteRequest_Start = DATEADD(dd, -(DATEPART(ww, @dteRequest) * 7) + 1, @dteRequest)
SET @dteRequest_End = DATEADD(dd, 1, @dteRequest)
--Get the start/End date for the prior year
SET @dteCompare_End = DATEADD(yy, -1, @dteRequest_End)
--If day is not sunday then add the appropriate number of days to it to get sunday
IF DATEPART(dw, @dteCompare_End) <> 7
BEGIN
SET @dteCompare_End = DATEADD(dd, 7 - DATEPART(dw, @dteCompare_End), @dteCompare_End)
END
--If week is not same week of year as Start then add or subtract week as appropriate
IF DATEPART(ww, DATEADD(dd, -1, @dteRequest_End)) <> DATEPART(ww, @dteCompare_End)
BEGIN
SET @dteCompare_End = DATEADD(wk, DATEPART(ww, DATEADD(dd, -1, @dteRequest_End)) - DATEPART(ww, @dteCompare_End), @dteCompare_End)
END
SET @dteCompare_Start = DATEADD(dd, -(DATEPART(ww, @dteCompare_End) * 7) + 1, @dteCompare_End)
SET @dteCompare_End = DATEADD(dd, 1, @dteCompare_End)