简体   繁体   English

SQL - 获取当前日期与一周开始和结束之间的所有日期

[英]SQL - Get all dates between Current day and start and end of week

I am trying to set the start day to Saturday我正在尝试将开始日期设置为星期六

SET DATEFIRST 6 -- Sets start day to Saturday

And then get all the GameDate between the start of the week and the end of the week.然后获取一周开始和一周结束之间的所有 GameDate。 But it needs to be done with the current day.但是需要在当天完成。 For instance: if the current day is thursday and the end of the week is Friday I don't want to do this:例如:如果当天是星期四,周末是星期五,我不想这样做:

DECLARE @StartWeek datetime Set @StartWeek = DATEADD(w, 0, DATEADD(w, DATEDIFF(w, 0,GETDATE()), -5))
DECLARE @EndWeek datetime Set @EndWeek = DATEADD(w, 0, DATEADD(w, DATEDIFF(w, 0,GETDATE()), 1))

Because this will make it that the next 5 days will get shown.因为这将显示接下来的 5 天。 And If the CurrentDay is on Thursday I don't want the next 5 days to get shown.如果 CurrentDay 是在星期四,我不希望显示接下来的 5 天。 Just the GameDates from every week.只是每周的 GameDates。

This is what I got so far:这是我到目前为止得到的:

DECLARE @DateTable Table (DateofWeek Date) -- Creates table
DECLARE @DateToday Date SELECT DAY(GETDATE()) 'Current Day' -- Gets current date

DECLARE @StartWeek datetime Set @StartWeek = DATEADD(w, 0, DATEADD(w, DATEDIFF(w, 0,GETDATE()), -5)) --This is the part thats wrong
DECLARE @EndWeek datetime Set @EndWeek = DATEADD(w, 0, DATEADD(w, DATEDIFF(w, 0,GETDATE()), 1)) --This is the part thats wrong

SET DATEFIRST 6 -- Sets start day to Saturday


SELECT DATEDIFF ( DAY, @DateToday, @EndWeek) AS Diffrence_End
SELECT DATEDIFF ( DAY, @DateToday, @StartWeek) AS Diffrence_Start
    FROM @DateTable

WHILE @DateToday >= @EndWeek AND @DateToday <= @StartWeek -- Shows all gameDates between StartWeek and Endweek
BEGIN   
    SELECT DATEDIFF ( DAY, @DateToday, @EndWeek)
    Insert Into @DateTable



SELECT * 
    FROM @DateTable
END;

Some of the Query is probably wrong, especially the last part.有些查询可能是错误的,尤其是最后一部分。

I hope I made myself clear enough, if there are any questions please don't hesitate to ask me!我希望我说得足够清楚,如果有任何问题,请不要犹豫,问我!

Have a look:看一看:

 -- Sets start day to Saturday SET DATEFIRST 6 -- Creates table DECLARE @DateTable Table ([DateofWeek] date); -- StartDate = DATEPART(DW, ... ) = 1 DECLARE @StartDate date; SET @StartDate = DATEADD(day, (DATEPART(dw, GETDATE()) - 1) * -1, GETDATE()); -- EndDate = StartDate + 6 days DECLARE @EndDate date; SET @EndDate = DATEADD(day, 6, @StartDate); -- Generates table values DECLARE @CurrentDate date; SET @CurrentDate = @StartDate; WHILE @CurrentDate <= @EndDate BEGIN INSERT INTO @DateTable ([DateofWeek]) VALUES (@CurrentDate); SET @CurrentDate = DATEADD(day, 1, @CurrentDate); END --Check it SELECT *, DATENAME(dw, DateOfWeek) as Name FROM @DateTable;

GO

\nDateofWeek |星期日期 | Name姓名     \n:------------------ | :------------------ | :-------- :--------\n18/03/2017 00:00:00 | 18/03/2017 00:00:00 | Saturday周六 \n19/03/2017 00:00:00 | 19/03/2017 00:00:00 | Sunday星期日   \n20/03/2017 00:00:00 | 20/03/2017 00:00:00 | Monday周一   \n21/03/2017 00:00:00 | 21/03/2017 00:00:00 | Tuesday周二  \n22/03/2017 00:00:00 | 22/03/2017 00:00:00 | Wednesday周三\n23/03/2017 00:00:00 | 23/03/2017 00:00:00 | Thursday周四 \n24/03/2017 00:00:00 | 24/03/2017 00:00:00 | Friday星期五   \n

dbfiddle here dbfiddle 在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM