I want to SET DATEFIRST in my function but it is not allowed.
SET DATEFIRST 1
I can add the code in a SP and call the SP from the function
If you need Monday as firstday follow this code snippet
declare @MyDate datetime = getdate() select CASE WHEN DATEPART(DW,@MyDate) = 1 THEN 7 WHEN DATEPART(DW,@MyDate) <= 7 THEN DATEPART(DW,@MyDate) - 1 END