I have the following T-SQL. I tried to use to calculate the date for the beginning of the week. I need my week to start on Monday:
SET DATEFIRST 1 -- Monday (First Week day)
DECLARE @dt DATETIME2
SELECT DATEADD(week, (-1) * (datepart(dw, @dt) - 1), @dt) AS datetime2--(7)
DECLARE @SchDT Datetime
DECLARE @TstPeriod varchar(20)
SET @TstPeriod = 'Week'
IF @TstPeriod = 'Month'
SET @SchDT = cast(DATEADD(Month, DATEDIFF(month,0,GETDATE()),-1) as datetime2(7))
IF @TstPeriod = 'Week'
SET @SchDT = cast(DATEADD(week, DATEDIFF(week,0,GETDATE()),-1) as datetime2(7))
SELECT @SchDT
Only after this, I realized that SQL Server considers the beginning of the week to be on Sunday - no matter what.
Has anyone come across a similar need and could share how to make SQL Server calculate datediff()
using Monday as beginning of the week?
Thank you very much