I have a Stored Function that it supposed to figure out my week start date for payroll. Which is “Thursday” However my stored procedure always gets my start date as “Friday” What is wrong with my declare and set functions? I want my StartWeekDay to be “Thursday”… “lStartday” is set to “5” in the account table My code is below:
DECLARE @StartWeekDay int
SET @StartWeekDay = (Select lStartDay From Account Where lLocationID = 1)
DECLARE @CurrentDate DateTime
SET @CurrentDate = GetDate()
DECLARE @CurrentWeekDay int
SET @CurrentWeekDay = DATEPART(dw,@CurrentDate)
DECLARE @Diff int
SET @Diff = @StartWeekDay - @CurrentWeekDay
SET @Diff = Case When @Diff > 0 Then -6 When @Diff = 0 Then 0 Else @Diff End
--
DECLARE @DaysToStart int
SET @DaysToStart = (DatePart(dw,@CurrentDate) - @StartWeekDay)
IF @DaysToStart <= .1
BEGIN
SET @DaysToStart = @DaysToStart + 7
END
DECLARE @myStartWeekDate DateTime
--SET @myStartWeekDate = dbo.DateOnly(DATEADD(d,@Diff,@CurrentDate))
SET @myStartWeekDate = Convert(nvarchar,DateAdd(d,-@DaysToStart,@CurrentDate),101) -- getting DATE only
--
lStartDay = 5 will always give u Friday. you need to add 4 for Thursday
If you are declaring @DaysToStart and DatePart also returns INT, why the comparison with .1??