Solved

T-SQL WorkingDay report

Posted on 2011-03-02
6
294 Views
Last Modified: 2012-05-11
I have a number of hours remaining on a task (Ex: 60 hours).

The working Day are Monday to Friday, we are estimating that we can only work 6 hours by working day.

ex:
2011-03-02 I have 60 hours remaining
so I have 10 working day left to work
The task should be finish on 2011-03-15


How can I calculate the completion date in SQL 2005?



-- SOMETHING LIKE THIS
DECLARE @RemainingHours FLOAT
SET @RemainingHours = 60
SELECT 
	DATEADD(DD, @RemainingHours / 6 * 1.40, GETDATE())

Open in new window

0
Comment
Question by:Cboudroz
6 Comments
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35019267
SELECT DATEDIFF(day,  GETDATE(), getdate()+3) * 10

this will return the days between two dates and multiply bt 10 hours per day
0
 
LVL 15

Expert Comment

by:derekkromm
ID: 35019418
DECLARE
      @RemainingHours FLOAT,
      @EndDate datetime,
      @NewEndDate datetime

SET @RemainingHours = 65

--add 1 week for every 30 hours, calculate remainder
select
      @EndDate = dateadd(ww, convert(int, @remaininghours / 30), getdate()-1),
      @RemainingHours = @RemainingHours - (30 * convert(int, @RemainingHours / 30))

select
      getdate(),
      @EndDate,
      @RemainingHours

--add extra days from remainder (rounding up, 1 extra hour = the following day)
select
      @NewEndDate = dateadd(dd, ceiling(@RemainingHours / 6), @EndDate)

--account for weekends
select
      @NewEndDate =
            case
                  when datepart(dw, @NewEndDate) = 7  -- if it is saturday, move to monday
                        or datepart(dw, @NewEndDate) < datepart(dw, @EndDate) then dateadd(dd, 2, @EndDate) -- if the weekend passed, then add 2 days since sat/sun don't count
                  else @NewEndDate
            end

select
      @NewEndDate


0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 35019544
I would modify derekkromm's response by adding 3 more input parameters, one for @HrsPerDay (INT Default = 6), one for @WorkSaturday (Char(1) Default = N) and one for @WorkSunday (Char(1) Default = N).  That would let you also cover cases where the pressure is really on and you are required (or you elect) to work Saturdays, Sundays, or both.  I would then use those to add @HrsPerDay or (@HrsPerDay * 2) in order to handle estimating the completion date based upon the additional information.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Author Comment

by:Cboudroz
ID: 35020782
derekkromm:

It's not working for some hours.

SET @RemainingHours = 19
...
SET @RemainingHours = 29


will give tomorrow date.
0
 
LVL 15

Accepted Solution

by:
derekkromm earned 500 total points
ID: 35020833
sorry, small typo in the case statement at the bottom (changed dateadd(dd, 2, @EndDate) to dateadd(dd, 2, @NewEndDate)

DECLARE 
      @RemainingHours FLOAT, 
      @EndDate datetime,
      @NewEndDate datetime

SET @RemainingHours = 29

--add 1 week for every 30 hours, calculate remainder
select 
      @EndDate = dateadd(ww, convert(int, @remaininghours / 30), getdate()-1),
      @RemainingHours = @RemainingHours - (30 * convert(int, @RemainingHours / 30))

select 
      getdate(),
      @EndDate,
      @RemainingHours

--add extra days from remainder (rounding up, 1 extra hour = the following day)
select 
      @NewEndDate = dateadd(dd, ceiling(@RemainingHours / 6), @EndDate)

--account for weekends
select
      @NewEndDate = 
            case
                  when datepart(dw, @NewEndDate) = 7  -- if it is saturday, move to monday
                        or datepart(dw, @NewEndDate) < datepart(dw, @EndDate) then dateadd(dd, 2, @NewEndDate) -- if the weekend passed, then add 2 days since sat/sun don't count
                  else @NewEndDate
            end

select
      @NewEndDate

Open in new window

0
 
LVL 7

Author Comment

by:Cboudroz
ID: 35021939
derekkromm:

Thanks for your help, I wasn't able to figure out the logic to used.

I created my function and everything work as expected.


ALTER FUNCTION fct_TaskEndDate_GET 
(
	-- Add the parameters for the function here
	@RemainingHours			FLOAT, 
	@WorkingHoursInWeek		INT,
	@StratingDate			SMALLDATETIME
)
RETURNS SMALLDATETIME
AS
BEGIN
	-- Declare the return variable here
	DECLARE @EndDate		SMALLDATETIME
	DECLARE @WeekEndDate	SMALLDATETIME

	IF @RemainingHours < 1
	BEGIN
		SET @EndDate = @StratingDate
		RETURN @EndDate

	END 

	
	--add 1 week for every @WorkingHoursInWeek hours
	SET	@WeekEndDate	= dateadd(ww, convert(int, @remaininghours / @WorkingHoursInWeek), @StratingDate-1)
	SET @RemainingHours = @RemainingHours - (30 * convert(int, @RemainingHours / 30))



	--add extra days from remainder (rounding up, 1 extra hour = the following day)
	SET @EndDate = dateadd(dd, ceiling(@RemainingHours / (convert(int, @WorkingHoursInWeek / 5) )), @WeekEndDate)


	--account for weekends
	SET @EndDate = 
					CASE 
						
						WHEN datepart(dw, @EndDate) = 7	-- if it is saturday, move to monday
						or datepart(dw, @EndDate) < datepart(dw, @WeekEndDate) then dateadd(dd, 2, @EndDate) -- if the weekend passed, then add 2 days since sat/sun don't count 
						ELSE @EndDate
					END


	-- Return the result of the function
	RETURN @EndDate

END
GO

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SLQ View not updating 10 47
SQL Server Log File Space 6 34
t-sql month question 8 43
combine an MS SQL string in Idera DM 9 31
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now