我想根据时间表表格运行每周报告(从星期一到星期日),这样我就可以看到正在申请和预订的职位类型。
在timesheets
表格中有start_dtm
和end_dtm
如何运行查询以检查一周的开始和结束?
编辑:我有以下代码来抓取开始日期和结束日期:
---------------------------------
--Please set your appropriate values for @REPORT_DATE and @WEEK_BEGINING
DECLARE @REPORT_DATE DATETIME, @WEEK_BEGINING VARCHAR(10)
SELECT @REPORT_DATE = '2012-03-16T00:00:00'
SELECT @WEEK_BEGINING = 'MONDAY'
IF @WEEK_BEGINING = 'MONDAY'
SET DATEFIRST 1
ELSE IF @WEEK_BEGINING = 'TUESDAY'
SET DATEFIRST 2
ELSE IF @WEEK_BEGINING = 'WEDNESDAY'
SET DATEFIRST 3
ELSE IF @WEEK_BEGINING = 'THURSDAY'
SET DATEFIRST 4
ELSE IF @WEEK_BEGINING = 'FRIDAY'
SET DATEFIRST 5
ELSE IF @WEEK_BEGINING = 'SATURDAY'
SET DATEFIRST 6
ELSE IF @WEEK_BEGINING = 'SUNDAY'
SET DATEFIRST 7
DECLARE @WEEK_START_DATE DATETIME, @WEEK_END_DATE DATETIME
--GET THE WEEK START DATE
SELECT @WEEK_START_DATE = @REPORT_DATE - (DATEPART(DW, @REPORT_DATE) - 1)
--GET THE WEEK END DATE
SELECT @WEEK_END_DATE = @REPORT_DATE + (7 - DATEPART(DW, @REPORT_DATE))
PRINT 'Week Start: ' + CONVERT(VARCHAR, @WEEK_START_DATE)
PRINT 'Week End: ' + CONVERT(VARCHAR, @WEEK_END_DATE)
----------------------------------------------
答案 0 :(得分:1)
答案 1 :(得分:1)
您可以使用此SQL来确定给定日期的星期一和星期五
SET DATEFIRST 1
DECLARE @Date date = GETDATE() --give a date here
SELECT
@Date [SelectedDate],
DATENAME(dw, @Date) [SelectedWD],
DATEADD(dd, -(DATEPART(dw, @Date)-1), @Date) [WeekStart],
DATENAME(dw, DATEADD(dd, -(DATEPART(dw, @Date)-1), @Date)) [WeekStartWD],
DATEADD(dd, 7-(DATEPART(dw, @Date)+2), @Date) [WeekEnd],
DATENAME(dw, DATEADD(dd, 7-(DATEPART(dw, @Date)+2), @Date)) [WeekEndWD]
**Example output**
SelectedDate SelectedWD WeekStart WeekStartWD WeekEnd WeekEndWD
2012-03-16 Friday 2012-03-12 Monday 2012-03-16 Friday
<强> -------------------------------------------- - 编辑---------------------------------------------
的 USAGE 强>
这为您提供1周(仅工作日)的结果
SET DATEFIRST 1
DECLARE @Date date = GETDATE() --give a date here
DECLARE @WeekStart Date, @WeekEnd Date
select @WeekStart = DATEADD(dd, -(DATEPART(dw, @Date)-1), @Date),
@WeekEnd = DATEADD(dd, 7-(DATEPART(dw, @Date)+2), @Date)
SELECT * FROM myTable WHERE myDate BETWEEN @WeekStart AND @WeekEnd