查找周开始和周结束日期
Finding week start and week end date
我有周数和年份,我需要在我的 ssrs 列中显示 "total for mm/dd/yy to mm/dd/yy in a row of my ssrs report. My week starts with Monday. For example if my week number is '2' and year is '2010' then I have to display "01/04/2010 到 01/10/2010 的总数。怎么做?
希望对您有所帮助:
declare @year char(4) = '2014'
declare @week int = 2
select dateadd(week,@week,convert(date,@year+'-01-01',121))
从此list
更改适合您的日期格式
SQL服务器在此之前有一个DATEPART function which calculates the ordinal week number of a year. However, you have to call DATEFIRST来定义一周中的哪一天代表一周的开始。在您的情况下,您已经声明一周的开始是星期一(即 1)。
SET DATEFIRST 1;
SELECT SUM([your data column])
FROM [your table]
WHERE DATEPART(WEEKNUM, [your date column])=[your week parameter]
AND DATEPART(YEAR, [your date column])=[your year parameter]
试试这个
declare @year char(4) = '2010'
declare @week int = 2
declare @fromdate datetime
declare @todate datetime
set @fromdate = DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @year) + (@week-1), 7);
set @todate = DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @year) + (@week-1), 6) ;
;WITH dates AS
(
SELECT CONVERT(datetime,@fromDate) as Date
UNION ALL
SELECT DATEADD(d,1,[Date])
FROM dates
WHERE DATE < @toDate
)
select * from dates
你的描述不是美国标准也不是isoweek。似乎是这些的混合体。我从来没有听说过这是一个标准。快等周了。这就是这个答案的基础。
计算iso年有点棘手,你可以阅读here:
这是您需要的语法:
DECLARE @year int = 2010
DECLARE @week int = 2
;WITH CTE AS
(
SELECT
dateadd(wk, datediff(wk, - @week * 7,
cast(cast(@year as char(4)) as datetime) - 5), 0) startofweek
)
SELECT
replace('total for ' + convert(char(10), startofweek, 110)
+ ' to ' + convert(char(10), dateadd(day, 6, startofweek) , 110), '-', '/')
FROM CTE
结果:
total for 01/11/2010 to 01/17/2010
2010 年第 2 周是 2010-01-11
尝试设置 DATEFIRST (https://msdn.microsoft.com/en-ie/library/ms181598.aspx)
SET DATEFIRST 7
declare @wk int
declare @yr int
declare @EndOfWeek as datetime
set @wk = 2
set @yr = 2010
SET @EndOfWeek = dateadd (week, @wk, dateadd (year, @yr-1900, 0)) + 1 - datepart(dw, dateadd (week, @wk, dateadd (year, @yr-1900, 0)) )
SELECT
replace('total for ' + convert(char(10), dateadd(day, -6, @EndOfWeek) , 110)
+ ' to ' + convert(char(10), @EndOfWeek, 110), '-', '/')
结果:
total for 01/04/2010 to 01/10/2010
我有周数和年份,我需要在我的 ssrs 列中显示 "total for mm/dd/yy to mm/dd/yy in a row of my ssrs report. My week starts with Monday. For example if my week number is '2' and year is '2010' then I have to display "01/04/2010 到 01/10/2010 的总数。怎么做?
希望对您有所帮助:
declare @year char(4) = '2014'
declare @week int = 2
select dateadd(week,@week,convert(date,@year+'-01-01',121))
从此list
更改适合您的日期格式SQL服务器在此之前有一个DATEPART function which calculates the ordinal week number of a year. However, you have to call DATEFIRST来定义一周中的哪一天代表一周的开始。在您的情况下,您已经声明一周的开始是星期一(即 1)。
SET DATEFIRST 1;
SELECT SUM([your data column])
FROM [your table]
WHERE DATEPART(WEEKNUM, [your date column])=[your week parameter]
AND DATEPART(YEAR, [your date column])=[your year parameter]
试试这个
declare @year char(4) = '2010'
declare @week int = 2
declare @fromdate datetime
declare @todate datetime
set @fromdate = DATEADD(wk, DATEDIFF(wk, 6, '1/1/' + @year) + (@week-1), 7);
set @todate = DATEADD(wk, DATEDIFF(wk, 5, '1/1/' + @year) + (@week-1), 6) ;
;WITH dates AS
(
SELECT CONVERT(datetime,@fromDate) as Date
UNION ALL
SELECT DATEADD(d,1,[Date])
FROM dates
WHERE DATE < @toDate
)
select * from dates
你的描述不是美国标准也不是isoweek。似乎是这些的混合体。我从来没有听说过这是一个标准。快等周了。这就是这个答案的基础。
计算iso年有点棘手,你可以阅读here:
这是您需要的语法:
DECLARE @year int = 2010
DECLARE @week int = 2
;WITH CTE AS
(
SELECT
dateadd(wk, datediff(wk, - @week * 7,
cast(cast(@year as char(4)) as datetime) - 5), 0) startofweek
)
SELECT
replace('total for ' + convert(char(10), startofweek, 110)
+ ' to ' + convert(char(10), dateadd(day, 6, startofweek) , 110), '-', '/')
FROM CTE
结果:
total for 01/11/2010 to 01/17/2010
2010 年第 2 周是 2010-01-11
尝试设置 DATEFIRST (https://msdn.microsoft.com/en-ie/library/ms181598.aspx)
SET DATEFIRST 7
declare @wk int
declare @yr int
declare @EndOfWeek as datetime
set @wk = 2
set @yr = 2010
SET @EndOfWeek = dateadd (week, @wk, dateadd (year, @yr-1900, 0)) + 1 - datepart(dw, dateadd (week, @wk, dateadd (year, @yr-1900, 0)) )
SELECT
replace('total for ' + convert(char(10), dateadd(day, -6, @EndOfWeek) , 110)
+ ' to ' + convert(char(10), @EndOfWeek, 110), '-', '/')
结果:
total for 01/04/2010 to 01/10/2010