Server/SQL Server
[SQL Server] 현재 주차및 주차에 해당하는 날짜 구하기
클리엘
2019. 9. 17. 11:30
728x90
-- 현재일에 해당하는 주차 -> 2019년 8월 12일은 8월달 몇주차인가?
Select CEILING((DAY('2019-08-12') + DATEPART(DW, '20190801') - 1) / 7.0)
-- 특정 주차의 날짜 -> 2019년 8월 3주차의 시작날짜와 끝날짜는?
Declare @input Varchar(8);
Set @input = '20190803';
Declare @firstday Date;
Set @firstday=CONVERT(Date, Left(@input, 4) + '-' + SUBSTRING(@input, 5, 2) + '-01');
Declare @addweek Int;
Set @addweek = CONVERT(Int, Right(@input, 2)) - 1;
Select DATEADD(DAY, @addweek * 7, firstweekdate) As startweekdate, DATEADD(DAY, @addweek * 7 + 6, firstweekdate) As endweekdate
From (
Select DATEADD(DAY, -(DATEPART(WEEKDAY, @firstday)) + 1, @firstday) As firstweekdate
) t;
728x90