Click here to Skip to main content
15,885,632 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Using the below Dateadd code to get my "Start Date" and "End Date", it works fine in returning those dates. But, I need to enhance/add code that will exclude Saturday and Sunday:

SQL
(left(convert(nvarchar, dateadd(day, -5, current_timestamp) , 120),11) + N'08:00:00') as [Start Date],
(left(convert(nvarchar, dateadd(day, -4, current_timestamp) , 120),11) + N'7:59:59.999') as [End Date],


I can use this in the Where clause, which does exclude Saturday and Sunday:

SQL
DATEPART(dw,[DateField]) not in (1,7)


But, was interested in seeing if a "Datepart" type statement could be added to my dateadd statement above?

Thanks,
jer

What I have tried:

I can use this in the Where clause, which does exclude Saturday and Sunday:

SQL
DATEPART(dw,[DateField]) not in (1,7)
Posted
Updated 27-Jul-20 23:47pm
Comments
ZurdoDev 27-Jul-20 13:02pm    
Google for examples of how to count business days. That will probably give you what you want.
Sandeep Mewara 28-Jul-20 1:31am    
sounds you re just looking for enhancing the dateadd line with something. or you are trying to find ways to just get work days?

1 solution

You could use CASE in the addition bit e.g.
SQL
declare @start date = '2020-01-01'
declare @end date = dateadd(dd, 10, @start)
declare @daystoadd int = 3
;WITH sampledatelist AS
(
    SELECT  @start AS datum
    UNION ALL
    SELECT  dateadd(DAY, 1, datum)
    FROM    sampledatelist
    WHERE dateadd(day, 1, datum) < @end
)
The CTE above just generates some dates. Here is the query that finds the next working day
SQL
select datum, -- the date we're trying to add to
DATEADD(DD, @daystoadd, datum) 
     AS JustAddedOn, -- as it says on the tin
DATEPART(DW, DATEADD(DD, @daystoadd, datum)) 
     AS DayOfJustAddedOn, -- what day of the week would that be
CASE WHEN DATEPART(DW, DATEADD(DD, @daystoadd, datum)) = 7 THEN 
      DATEADD(DD, @DAYSTOADD + 2, DATUM)	-- Handle Saturdays
WHEN DATEPART(DW, DATEADD(DD, @daystoadd, datum)) = 1 THEN 
      DATEADD(DD, @DAYSTOADD + 1, DATUM)	-- Handle Sundays
ELSE 
      DATEADD(DD, @daystoadd, datum) -- otherwise use the JustAddedOn date
END as calcdate
from sampledatelist
which gives results
datum		JustAddedOn	Day	calcdate
2020-01-01	2020-01-04	7	2020-01-06
2020-01-02	2020-01-05	1	2020-01-06
2020-01-03	2020-01-06	2	2020-01-06
2020-01-04	2020-01-07	3	2020-01-07
2020-01-05	2020-01-08	4	2020-01-08
2020-01-06	2020-01-09	5	2020-01-09
2020-01-07	2020-01-10	6	2020-01-10
2020-01-08	2020-01-11	7	2020-01-13
2020-01-09	2020-01-12	1	2020-01-13
2020-01-10	2020-01-13	2	2020-01-13
Caveats
- check your localisation settings for what constitutes a weekend for your part of the world!
- This doesn't handle national holidays etc

Incidentally, I personally intensely dislike converting dates to strings just to add a time (that's just a me thing). I prefer to add the required number of hours (or in this case seconds) to the "start" of the day in question e.g.
SQL
declare @starttime INT = DATEDIFF(SECOND,0,CAST('08:00:00' AS datetime)) -- End Time will be 1 second earlier

select dateadd(second, @starttime, dateadd(dd, datediff(dd, 0, dateadd(day, -5, current_timestamp)), 0)) as [Start Date],
       dateadd(second, @starttime - 1, dateadd(dd, datediff(dd, 0, dateadd(day, -4, current_timestamp)), 0)) as [End Date]

Lastly, if I was doing a lot of comparing dates with workdays and holidays I would build up a date table containing all the useful stuff such as date, day of the week, quarter, financial period, working day etc etc then use that a reference in other queries, joining on date. You could even have a pre-calculated "next working day" column
 
Share this answer
 
Comments
jr7138 28-Jul-20 10:57am    
CHill60:

The above works well. Thank you.

On your last recommendation. Is there code we can add to NOT count Saturday and Sunday?

Example: If I ran today, 07/28/2020, I need for my:

[Start Date] = 7/22/2020
[End Date] = 07/23/2020

Thanks again for you help.

Regards,
jer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900