有没有办法可以根据另一个参数列表(年份)创建周(param):w1,W2,...
周列表,因此第一个列表是年份,第二个列表是与所选年份对应的周数。< / p>
例如,如果我从第一个列表中选择2017,则应使用标签W1,W2,...更新第二个列表(周)。这些值是给定年份的相应日期。
答案 0 :(得分:0)
您可以对此进行调整以在SQL中创建表,然后在报表中引用它。 注意:如果您需要超过一年的日期,则可以在外部循环中包含Year
declare @year as int
declare @startdate as datetime
declare @wk as int
declare @tbl as table (
tbl_wk int,
tbl_Monday datetime
)
set @wk = 1
set @year = '2017'
set @startdate = CAST (cast(@year as varchar(4))+ '/01/01 00:00:00' as datetime)
while (@wk < 53)
begin
insert into @tbl (tbl_wk,tbl_Monday)
select @wk,DATEADD(wk, DATEDIFF(wk,0,@startdate), 0) as monday--MondayOfCurrentWeek,
set @wk = @wk+1
set @startdate = @startdate+7
end
select * from @tbl
答案 1 :(得分:0)
试试这个......
declare @year as int
declare @startdate as datetime
declare @wk as int
declare @endwk as datetime
declare @tbl as table (
tbl_wk int,
tbl_Monday datetime
)
set @endwk= (SELECT DATEPART(wk, GETDATE())+1)
set @wk = 1
set @year = '2017'
set @startdate = CAST (cast(@year as varchar(4))+ '/01/01 00:00:00' as datetime)
while (@wk < @endwk)
begin
insert into @tbl (tbl_wk,tbl_Monday)
select @wk,DATEADD(wk, DATEDIFF(wk,0,@startdate), 0) as monday--MondayOfCurrentWeek,
set @wk = @wk+1
set @startdate = @startdate+7
end
select * from @tbl