Следующий код получит дату понедельника недели указанной даты независимо от значения DateFirst
или Language
:
Cast( DateAdd( day, - ( @@DateFirst + DatePart( weekday, Datum ) - 2 ) % 7, Datum ) as Date )
Пример с данными примера:
with SampleData as (
select GetDate() - 30 as Datum
union all
select DateAdd( day, 1, Datum )
from SampleData
where Datum < GetDate() )
select Datum,
-- 1 = Monday through 7 = Sunday.
( @@DateFirst + DatePart( weekday, Datum ) - 2 ) % 7 + 1 as WeekDay,
-- Date of Monday in the week of the supplied date.
Cast( DateAdd( day, - ( @@DateFirst + DatePart( weekday, Datum ) - 2 ) % 7, Datum ) as Date ) as Monday
from SampleData;