Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>--SQL sets the first day of the week as sunday and for our purposes we want it to be Monday.<br> --This command does that.</p> <pre><code>SET DATEFIRST 1 DECLARE @ReportDate DATETIME, @Weekday INTEGER, @NumDaysToMonday INTEGER, @MondayStartPoint DATETIME, @MondayStartPointWeek INTEGER, @DateToProcess DATETIME, @DateToProcessWeek INTEGER, @Bucket VARCHAR(50), @DaysDifference INTEGER, @BucketNumber INTEGER, @NumDaysToMondayOfDateToProcess INTEGER, @WeekdayOfDateToProcess INTEGER, @MondayOfDateToProcess DATETIME, @SundayOfDateToProcess DATETIME SET @ReportDate = '2009-01-01' print @ReportDate SET @DateToProcess = '2009-01-26' --print @DateToProcess SET @Weekday = (select DATEPART ( dw , @ReportDate )) --print @Weekday --print DATENAME(dw, @ReportDate) SET @NumDaysToMonday = (SELECT CASE WHEN @Weekday = 1 THEN 0 WHEN @Weekday = 2 THEN 1 WHEN @Weekday = 3 THEN 2 WHEN @Weekday = 4 THEN 3 WHEN @Weekday = 5 THEN 4 WHEN @Weekday = 6 THEN 5 WHEN @Weekday = 7 THEN 6 END) --print @NumDaysToMonday SET @MondayStartPoint = (SELECT DATEADD (d , -1*@NumDaysToMonday, @ReportDate)) --print @MondayStartPoint SET @DaysDifference = DATEDIFF ( dd , @MondayStartPoint , @DateToProcess ) --PRINT @DaysDifference SET @BucketNumber = @DaysDifference/7 --print @BucketNumber ----Calculate the start and end dates of this bucket------ PRINT 'Start Of New Calc' print @DateToProcess SET @WeekdayOfDateToProcess = (select DATEPART ( dw , @DateToProcess )) print @WeekdayOfDateToProcess SET @NumDaysToMondayOfDateToProcess= (SELECT CASE WHEN @WeekdayOfDateToProcess = 1 THEN 0 WHEN @WeekdayOfDateToProcess = 2 THEN 1 WHEN @WeekdayOfDateToProcess = 3 THEN 2 WHEN @WeekdayOfDateToProcess = 4 THEN 3 WHEN @WeekdayOfDateToProcess = 5 THEN 4 WHEN @WeekdayOfDateToProcess = 6 THEN 5 WHEN @WeekdayOfDateToProcess = 7 THEN 6 END) print @NumDaysToMondayOfDateToProcess SET @MondayOfDateToProcess = (SELECT DATEADD (d , -1*@NumDaysToMondayOfDateToProcess, @DateToProcess)) print @MondayOfDateToProcess ---This is the start week SET @SundayOfDateToProcess = (SELECT DATEADD (d , 6, @MondayOfDateToProcess)) PRINT @SundayOfDateToProcess </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload