如何从SQL Server中的星期编号获取“星期开始date”和“星期结束date”?
我有一个查询,数据库中的成员的婚礼date…
Select Sum(NumberOfBrides) As [Wedding Count], DATEPART( wk, WeddingDate) as [Week Number], DATEPART( year, WeddingDate) as [Year] FROM MemberWeddingDates Group By DATEPART( year, WeddingDate), DATEPART( wk, WeddingDate) Order By Sum(NumberOfBrides) Desc
如何在结果集中表示每周的开始和结束?
Select Sum(NumberOfBrides) As [Wedding Count], DATEPART( wk, WeddingDate) as [Week Number], DATEPART( year, WeddingDate) as [Year], ??? as WeekStart, ??? as WeekEnd FROM MemberWeddingDates Group By DATEPART( year, WeddingDate), DATEPART( wk, WeddingDate) Order By Sum(NumberOfBrides) Desc
您可以查找星期几,并在date中添加date以获取开始date和结束date..
DATEADD(dd, -(DATEPART(dw, WeddingDate)-1), WeddingDate) [WeekStart] DATEADD(dd, 7-(DATEPART(dw, WeddingDate)), WeddingDate) [WeekEnd]
你可能也想看看从date剥离的时间,以及虽然。
这是一个DATEFIRST
不可知的解决scheme:
SET DATEFIRST 4 /* or use any other weird value to test it */ DECLARE @d DATETIME SET @d = GETDATE() SELECT @d ThatDate, DATEADD(dd, (@@DATEFIRST + 5 + DATEPART(dw, @d)) % 7, @d) Monday, DATEADD(dd, 6 - (@@DATEFIRST + 5 + DATEPART(dw, @d)) % 7, @d) Sunday
你也可以使用这个:
SELECT DATEADD(day, DATEDIFF(day, 0, WeddingDate) /7*7, 0) AS weekstart, DATEADD(day, DATEDIFF(day, 6, WeddingDate-1) /7*7 + 7, 6) AS WeekEnd
这是另一个版本。 如果您的scheme要求星期六是星期和星期五的第一天,并且是星期的最后一天,则下面的代码将处理:
DECLARE @myDate DATE = GETDATE() SELECT @myDate, DATENAME(WEEKDAY,@myDate), DATEADD(DD,-(CHOOSE(DATEPART(dw, @myDate), 2,3,4,5,6,7,1)-1),@myDate) AS WeekStartDate, DATEADD(DD,7-CHOOSE(DATEPART(dw, @myDate), 2,3,4,5,6,7,1),@myDate) AS WeekEndDate
下面的查询将给出本周从周日到周六开始和结束之间的数据
SELECT DOB FROM PROFILE_INFO WHERE DAY(DOB) BETWEEN DAY( CURRENT_DATE() - (SELECT DAYOFWEEK(CURRENT_DATE())-1)) AND DAY((CURRENT_DATE()+(7 - (SELECT DAYOFWEEK(CURRENT_DATE())) ) )) AND MONTH(DOB)=MONTH(CURRENT_DATE())
扩展@ Tomalak的答案。 公式适用于星期日和星期一以外的日子,但是您需要在5的位置使用不同的值。 达到你需要的价值的一种方法是
Value Needed = 7 - (Value From Date First Documentation for Desired Day Of Week) - 1
这里是一个链接到文档: https : //msdn.microsoft.com/en-us/library/ms181598.aspx
这是一张桌子,为您准备。
| DATEFIRST VALUE | Formula Value | 7 - DATEFIRSTVALUE - 1 Monday | 1 | 5 | 7 - 1- 1 = 5 Tuesday | 2 | 4 | 7 - 2 - 1 = 4 Wednesday | 3 | 3 | 7 - 3 - 1 = 3 Thursday | 4 | 2 | 7 - 4 - 1 = 2 Friday | 5 | 1 | 7 - 5 - 1 = 1 Saturday | 6 | 0 | 7 - 6 - 1 = 0 Sunday | 7 | -1 | 7 - 7 - 1 = -1
但是你不必记住那张表和公式,实际上你也可以使用一个稍微不同的expression式,主要的需要是使用一个值,这个值可以使余数为正确的天数。
这是一个工作的例子:
DECLARE @MondayDateFirstValue INT = 1 DECLARE @FridayDateFirstValue INT = 5 DECLARE @TestDate DATE = GETDATE() SET @MondayDateFirstValue = 7 - @MondayDateFirstValue - 1 SET @FridayDateFirstValue = 7 - @FridayDateFirstValue - 1 SET DATEFIRST 6 -- notice this is saturday SELECT DATEADD(DAY, 0 - (@@DATEFIRST + @MondayDateFirstValue + DATEPART(dw,@TestDate)) % 7, @TestDate) as MondayStartOfWeek ,DATEADD(DAY, 6 - (@@DATEFIRST + @MondayDateFirstValue + DATEPART(dw,@TestDate)) % 7, @TestDate) as MondayEndOfWeek ,DATEADD(DAY, 0 - (@@DATEFIRST + @FridayDateFirstValue + DATEPART(dw,@TestDate)) % 7, @TestDate) as FridayStartOfWeek ,DATEADD(DAY, 6 - (@@DATEFIRST + @FridayDateFirstValue + DATEPART(dw,@TestDate)) % 7, @TestDate) as FridayEndOfWeek SET DATEFIRST 2 --notice this is tuesday SELECT DATEADD(DAY, 0 - (@@DATEFIRST + @MondayDateFirstValue + DATEPART(dw,@TestDate)) % 7, @TestDate) as MondayStartOfWeek ,DATEADD(DAY, 6 - (@@DATEFIRST + @MondayDateFirstValue + DATEPART(dw,@TestDate)) % 7, @TestDate) as MondayEndOfWeek ,DATEADD(DAY, 0 - (@@DATEFIRST + @FridayDateFirstValue + DATEPART(dw,@TestDate)) % 7, @TestDate) as FridayStartOfWeek ,DATEADD(DAY, 6 - (@@DATEFIRST + @FridayDateFirstValue + DATEPART(dw,@TestDate)) % 7, @TestDate) as FridayEndOfWeek
这种方法将不可知的DATEFIRST
设置,这是我所需要的,因为我正在构build一个date维度包括多个星期的方法。
我只是遇到类似的情况,但这里的解决scheme似乎没有帮助我。 所以我试图自己弄清楚。 我只在周开始date,周末date应该是类似的逻辑。
Select Sum(NumberOfBrides) As [Wedding Count], DATEPART( wk, WeddingDate) as [Week Number], DATEPART( year, WeddingDate) as [Year], DATEADD(DAY, 1 - DATEPART(WEEKDAY, dateadd(wk, DATEPART( wk, WeddingDate)-1, DATEADD(yy,DATEPART( year, WeddingDate)-1900,0))), dateadd(wk, DATEPART( wk, WeddingDate)-1, DATEADD(yy,DATEPART( year, WeddingDate)-1900,0))) as [Week Start] FROM MemberWeddingDates Group By DATEPART( year, WeddingDate), DATEPART( wk, WeddingDate) Order By Sum(NumberOfBrides) Desc
除了第一周和一周的最后一周之外,表决得分最高的答案是正常的。 例如,如果WeddingDate的值是“2016-01-01”,结果将是2015-12-27和2016-01-02 ,但正确答案是2016-01-01和2016-01-02 。
尝试这个:
Select Sum(NumberOfBrides) As [Wedding Count], DATEPART( wk, WeddingDate) as [Week Number], DATEPART( year, WeddingDate) as [Year], MAX(CASE WHEN DATEPART(WEEK, WeddingDate) = 1 THEN CAST(DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0) AS date) ELSE DATEADD(DAY, 7 * DATEPART(WEEK, WeddingDate), DATEADD(DAY, -(DATEPART(WEEKDAY, DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0)) + 6), DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0))) END) as WeekStart, MAX(CASE WHEN DATEPART(WEEK, WeddingDate) = DATEPART(WEEK, DATEADD(DAY, -1, DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate) + 1, 0))) THEN DATEADD(DAY, -1, DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate) + 1, 0)) ELSE DATEADD(DAY, 7 * DATEPART(WEEK, WeddingDate) + 6, DATEADD(DAY, -(DATEPART(WEEKDAY, DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0)) + 6), DATEADD(YEAR, DATEDIFF(YEAR, 0, WeddingDate), 0))) END) as WeekEnd FROM MemberWeddingDates Group By DATEPART( year, WeddingDate), DATEPART( wk, WeddingDate) Order By Sum(NumberOfBrides) Desc;
结果如下所示:
它适用于所有的周,第一或其他。
不知道这是多么有用,但我最终在这里寻找Netezza SQL的解决scheme,并找不到一个堆栈溢出。
对于IBM netezza,你会使用一些东西(周开始周,周末太阳),如:
selectnext_day(WeddingDate,'SUN')-6作为WeekStart,
next_day(WeddingDate,'SUN')为WeekEnd
对于Access查询,您可以在下面的格式中使用字段
"FirstDayofWeek:IIf(IsDate([ForwardedForActionDate]),CDate(Format([ForwardedForActionDate],"dd/mm/yyyy"))-(Weekday([ForwardedForActionDate])-1))"
直接计算允许..