이것은 내 구현이며 완벽하게 작동합니다. SQL datetime (1753 년)에서 요청한 날짜의 첫 번째 발생을 찾고 선택한 연도/월에 첫 번째 발생까지 추가 할 일 수를 계산합니다. 그런 다음 x 주를 해당 날짜에 추가 한 결과를 선택한 테스트 케이스에서 테스트하여 선택한 달 내에 항상 도착하도록합니다.
이것은 SQL 데이터베이스에서 UTC 날짜 : 시간에 대해 생각해 본 사람들에게 매우 유용합니다. '결과의 경과 시간'을 그래프로 표시하고 그 시간이 일광 바이어스를 알고 있고 시작/종료 시점을 알면 몇 개월에 걸쳐 쿼리에 사용할 수없는 정보입니다.
Create function dbo.DST_From_MSStandardDate
(
@year int,
@month int,
@week int,
@hour int,
-- Sun = 1, Mon = 2, Tue = 3, Wed = 4
-- Thu = 5, Fri = 6, Sat = 7
-- Default to Sunday
@day int = 1
)
/*
Find the first day matching @day in the month-year
requested. Then add the number of weeks to a minimum
of start of the month and maximum of end of the month
*/
returns datetime
as
begin
declare @startPoint datetime
declare @finalPoint datetime
declare @firstDayInMonth datetime
declare @begin_week datetime
-- Create the base date
set @startPoint = dateadd(mm,(@year-1900)* 12 + @month - 1,0)
-- Check for valid day of week
if @day between 1 and 7
begin
-- Find first day on or after 1753/1/1 (-53690)
-- matching day of week of @day
select @begin_week = convert(datetime,-53690+((@day+5)%7))
-- Verify beginning of week not before 1753/1/1
if @startPoint >= @begin_week
begin
select @firstDayInMonth = dateadd(dd,(datediff(dd,@begin_week,@startPoint)/7)*7,@begin_week)
end
end
-- Case for an offset, some weeks have 5 weeks, others have 4 weeks.
set @finalPoint = dateadd(hour,@hour,dateadd(wk,@week-
case
when datepart(month,dateadd(wk,@week,@firstDayInMonth))>@month then 1 -- day lands in the following month
when datepart(month,dateadd(wk,@week,@firstDayInMonth))<@month then 0 -- day lands in the proceeding month
else 0
end
,@firstDayInMonth))
return @finalPoint
end
go
당신은 일 및 월이 있습니다 - 수행해야 할 계산은 주 및 일 수에 대한 것입니다. –