Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS 2008 Datediff for Working Days

I have seen this question asked a lot and I cannot seem to find one clear answer about

"how to calculate business days only between two dates?"

The expression below will give me the total days but I am looking to exclude Saturday and Sunday.

=DateDiff("d",Parameters!STARTDATE.Value,Parameters!ENDDATE.Value)   

I would appreciate specific help about how to accomplish this.

Thank you in advance.

like image 429
Lance Avatar asked Feb 11 '14 16:02

Lance


People also ask

How do I use datediff in SSRS?

The SSRS DateDiff function is used to determine the distinction between different dates and generate results in a selected date part or increment including such days, hours, or minutes. The SQL Server function DATEDIFF() is used to perform date computation.

How do you use datediff?

To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1.


2 Answers

This code is not exactly correct. A year can start or end with either a Saturday or a Sunday. For example, 2011 starts on a Saturday and ends on a Saturday. January 1st & 2nd, 2011 are Saturday and Sunday respectively and Dec 31st, 2011 is also a Saturday. The above code does not account for this scenario. The code below is correct:

= (DateDiff(DateInterval.day,Parameters!BeginDate.Value,Parameters!EndDate.Value)+1)
- (DateDiff(DateInterval.WeekOfYear,Parameters!BeginDate.Value,Parameters!EndDate.Value)*2) 
- IIF(Weekday(Parameters!BeginDate.Value,1) = 1,1,0)
- IIF(Weekday(Parameters!BeginDate.Value,1) = 7,1,0)
- IIF(Weekday(Parameters!EndDate.Value,1) = 1,1,0)
- IIF(Weekday(Parameters!EndDate.Value,1) = 7,1,0)
like image 126
Charles M. Avatar answered Sep 22 '22 15:09

Charles M.


The SQL in the link (Number of working days between two dates) translated for SSRS: Hopefully this will give you a good place to start. Type this into the expression for the textbox.

=(DateDiff(DateInterval.day,Parameters!STARTDATE.Value,Parameters!ENDDATE.Value)+1)
-(DateDiff(DateInterval.WeekOfYear,Parameters!STARTDATE.Value,Parameters!ENDDATE.Value)*2)
-(iif(Weekday(Parameters!STARTDATE.Value) = 7,1,0)
-(iif(Weekday(Parameters!ENDDATE.Value) = 6,1,0))-1)
like image 28
Pops Avatar answered Sep 24 '22 15:09

Pops