I have a query for calculating first and last date in the week, according to given date. It is enough to set @dDate
and the query will calculate first (monday) and last date (sunday) for that week.
Problem is, that is calculating wrong and I don't understand why.
Example:
@dDate = 2019-10-03 (year-month-day).
Result:
W_START W_END
2019-09-25 2019-10-01
But it should be:
2019-09-30 2019-10-06
Why is that?
Query:
set datefirst 1
declare @dDate date = cast('2019-10-16' as date)
select @dDAte
declare @year int = (select DATEPART(year, @dDAte))
select @year
declare @StartingDate date = cast(('' + cast(@year as nvarchar(4)) + '-01-01') as date)
select @StartingDate
declare @dateWeekEnd date = (select DATEADD(week, (datepart(week, cast(@dDate as date)) - 1), @StartingDate))
declare @dateWeekStart date = dateadd(day, -6, @dateWeekEnd)
select @dateWeekStart W_START, @dateWeekEnd W_END