I need to create a temp table that will have a Start_Date and an End_Date column based on input parameters sdate and edate. This table is the list of the off-hours of the office which includes 6pm to 6am on weekdays. For the weekends i need the time to be as follows :
- 6pm Friday to 12am Saturday
- 12am Saturday to 12am Sunday and
- 12am Sunday to 6am Monday
Hope the following table makes you understand what i am trying to achieve :
| Start | Start_Date | End | End_Date |
|---|---|---|---|
| Sun | 08/01/2021 00:00:00.000 | Mon | 08/02/2021 06:00:00.000 |
| Mon | 08/02/2021 18:00:00.000 | Tue | 08/03/2021 06:00:00.000 |
| Tue | 08/03/2021 18:00:00.000 | Wed | 08/04/2021 06:00:00.000 |
| Wed | 08/04/2021 18:00:00.000 | Thu | 08/05/2021 06:00:00.000 |
| Thu | 08/05/2021 18:00:00.000 | Fri | 08/06/2021 06:00:00.000 |
| Fri | 08/06/2021 18:00:00.000 | Sat | 08/07/2021 00:00:00.000 |
| Sat | 08/07/2021 00:00:00.000 | Sun | 08/08/2021 00:00:00.000 |
| Sun | 08/08/2021 00:00:00.000 | Mon | 08/09/2021 06:00:00.000 |
| Mon | 08/09/2021 18:00:00.000 | Tue | 08/10/2021 06:00:00.000 |
| Tue | 08/10/2021 18:00:00.000 | Wed | 08/11/2021 06:00:00.000 |
I have tried the following code, but it does not get the timeframe right.
ALTER PROCEDURE [dbo].[temptableforoffhours]
@BDate varchar(50),
@EDate varchar(50),
@Provider varchar(1000)=''
AS
BEGIN
SET NOCOUNT ON
DECLARE
@BeginDate datetime = '',
@EndDate DATETIME = ''
IF @BeginDate = '' AND @EndDate = ''
BEGIN
SET @BeginDate = DATEADD(YY,-3,GETDATE())
SET @EndDate = GETDATE()
END
IF @BeginDate <> '' AND @EndDate <> ''
BEGIN
Set @BeginDate =CONVERT(datetime, @BDate, 121)
SET @EndDate =Convert (Datetime, cast(CONVERT(DateTime, DATEADD(DD,1,@EDate), 121) as Date),121)
END
/********************************Creation of #tmptimeFrameAudit table with FrameID and Start_day and end_Day********************************/
Declare @CountTimeFrames int = DateDiff(Day, @BeginDate, @EndDate)
DECLARE @Counter INT
SET @Counter = 1
Create Table #tmptimeFrameAudit (Frameid int,Start_Day datetime, End_Day datetime, doW varchar(10))
WHILE ( @Counter <= @CountTimeFrames)
BEGIN
IF @counter = 1
Begin
set @BeginDate = @BeginDate
End
Else
Begin
set @BeginDate = DATEADD(DD,1,@BeginDate)
End
IF (DatePart(weekday,@BeginDate) = 7)
BEGIN
Insert Into #tmptimeFrameAudit values (@Counter,@BeginDate,DATEADD(HOUR,24,@BeginDate), DATENAME(WEEKDAY,@BeginDate))
END
ELSE IF (DatePart(weekday,@BeginDate) = 6)
BEGIN
Insert Into #tmptimeFrameAudit values (@Counter,Dateadd(HOUR,18,CONVERT(datetime, @BeginDate, 121)),DATEADD(HOUR,6,@BeginDate), DATENAME(WEEKDAY,@BeginDate))
END
ELSE IF (DatePart(weekday,@BeginDate) = 1)
BEGIN
Insert Into #tmptimeFrameAudit values (@Counter,@BeginDate,DATEADD(HOUR,30,@BeginDate), DATENAME(WEEKDAY,@BeginDate))
END
ELSE
BEGIN
Insert Into #tmptimeFrameAudit values (@Counter,Dateadd(HOUR,18,CONVERT(datetime, @BeginDate, 121)),DATEADD(HOUR,12,@BeginDate), DATENAME(WEEKDAY,@BeginDate))
END
set @Counter = @Counter+1
END
Select * from #tmptimeFrameAudit
drop Table #tmptimeFrameAudit
END
Can you please tell me what is wrong with the code and help me rectify it? Thanks!
Not sure I understand the point of your Provider parameter. It is not referenced in your code at all. I would also strongly suggest you pass the proper datatypes instead of strings.
I would suggest that using a calendar table is a better approach because you have the flexibility of things like holiday days off, or even extended or shortened hours on certain days. But that is outside the scope of what you asked.
I am using a tally table here instead of a loop. I keep one on my databases as a view like this.
Now we can utilize this tally table for a set based approach to this problem.
This returns the results you showed in your question.