我有一个输入:@dateInput
。
例如:@dateInput = 2013/10/02
(格式:yyyy / MM / dd)。
如何通过T-SQL
获得10月份的周(周一至周五)StartWeek EndWeek
2013-10-01 00:00:00.000 2013-10-05 00:00:00.000
2013-10-07 00:00:00.000 2013-10-12 00:00:00.000
2013-10-14 00:00:00.000 2013-10-19 00:00:00.000
2013-10-21 00:00:00.000 2013-10-26 00:00:00.000
2013-10-28 00:00:00.000 2013-10-31 00:00:00.000
全部谢谢!
答案 0 :(得分:1)
如果你大致知道一年中你可以生成一年和使用它们的配对
declare @input datetime
set @input = '20131002'
declare @monday datetime
set @monday='20121231'
declare @beginOfMonth datetime
declare @endOfMonth datetime
set @beginOfMonth = Substring(Convert(char,@input,102),1,8)+'01'
set @endOfMonth = DateAdd(month,1,@beginOfMonth)-1
declare @i int
set @i=1
create table #week
(StartDate datetime,
EndDate datetime)
while (@i <= 52)
begin
insert into #week
values (@monday, DateAdd(day,5,@monday))
set @monday=DateAdd(week,1,@monday)
set @i=@i+1
end
select * from #week
where StartDate between @beginOfMonth and @endOfMonth and EndDate between @beginOfMonth and @endOfMonth
drop table #week