Introduction
We are trying to get the time (HH:MM:SS) between Task Started Date and End Date (for example,
office timings used to start at 7:30 AM and end at 6:30 PM). If we want to extract
a particular
time for a particular task just send START DATE and END DATE of task to this procedure. It will
give the exact time. Another important point is we need to remove weekends (Saturday and Sunday).
Here I am considering only weekdays, which means only working days.
Using the code
Here I am dividing the solution into two conditions: Task finished on same date and Task
finished on different dates.
Check the following procedure and send two DateTime values to these procedures and they will
return the exact time (HH:MM:SS).
USE [DATABASENAME]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GETTIME](@STARTDATE Datetime,
@ENDDATE Datetime,@result varchar(500) OUTPUT)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
DECLARE @DayNameCheck varchar(250),@daynum1 int,@daynum2 int,@diffHours int,@dayDifday int;
SELECT @daynum1=(SELECT datepart(WEEKDAY,@STARTDATE));
SELECT @daynum2=(SELECT datepart(WEEKDAY,@ENDDATE));
SELECT @diffHours=(SELECT DATEDIFF(HH, @STARTDATE,@ENDDATE));
SELECT @dayDifday=(SELECT DATEDIFF(day,@STARTDATE,@ENDDATE));
IF((SELECT(CONVERT(DATE,@STARTDATE)))= (SELECT(CONVERT(DATE,@ENDDATE))))
BEGIN
DECLARE @getsecond int;
SELECT @getsecond=DATEDIFF(SECOND,@STARTDATE, @ENDDATE);
SELECT @result=(SELECT CAST(@getsecond/3600 AS VARCHAR(10))+
RIGHT(CONVERT(CHAR(8),DATEADD(ss,@getsecond,0),108),6));
END
ELSE IF((SELECT(CONVERT(DATE,@STARTDATE)))!=(SELECT(CONVERT(DATE,@ENDDATE))))
BEGIN
DECLARE @Date DATE,@gg DATE,@cc Date,@val int=0,@getseconds int=0,
@getstart Time,@EndTime Time='18:30:00.000',@StartTime Time='07:30:00.000',@getweek int;
SELECT @Date=(SELECT Convert(DATE,@STARTDATE));
SELECT @getstart=(SELECT Convert(TIME,@STARTDATE));
SELECT @getseconds=@getseconds+DATEDIFF(SECOND,(CAST (@Date AS DATETIME) +
@getstart),(CAST (@Date AS DATETIME) + @EndTime))
WHILE(@val<@dayDifday)
BEGIN
SELECT @Date=(SELECT Convert(DATE,DATEADD(day,1,@Date)));
SELECT @getweek=(SELECT datepart(WEEKDAY,@Date));
IF(@getweek!=1 AND @getweek!=7)
BEGIN
IF(@Date!=Convert(DATE,@ENDDATE))
BEGIN
SELECT @getseconds=@getseconds+DATEDIFF(SECOND,(CAST (@Date AS DATETIME) +
@StartTime),(CAST (@Date AS DATETIME) + @EndTime))
END
ELSE IF(@Date=Convert(DATE,@ENDDATE))
BEGIN
DECLARE @tt Time;
SELECT @tt=CONVERT(Time,@ENDDATE)
SELECT @getseconds=@getseconds+DATEDIFF(SECOND,(CAST
(Convert(DATE,@ENDDATE) AS DATETIME) + @StartTime),
(CAST (Convert(DATE,@ENDDATE) AS DATETIME) + @tt))
BREAK;
END
END
SET @val=@val+1;
END
SELECT @result=(SELECT CAST(@getseconds/3600 AS VARCHAR(10))+
RIGHT(CONVERT(CHAR(8),DATEADD(ss,@getseconds,0),108),6));
END
end try
begin catch
DECLARE @ErrorNumber INT = ERROR_NUMBER()
DECLARE @ErrorMessage NVARCHAR(4000) = ERROR_MESSAGE()
DECLARE @ErrorProcedure NVARCHAR(4000) = ERROR_PROCEDURE()
DECLARE @ErrorLine INT = ERROR_LINE()
RAISERROR ('An error occurred within a user transaction.
Error Number : %d
Error Message : %s
Affected Procedure : %s
Affected Line Number: %d', 16, 1, @ErrorNumber, @ErrorMessage,@ErrorProcedure,@ErrorLine)
end catch
END