Calculate total Working hours from sql column

sql query to calculate working hours
sql working days in month
how to calculate total time in sql
calculate working days between two dates in sql server
how to calculate night shift hours in sql server
datediff sql
calculate hours between two dates in sql
how to calculate day in sql server

Expected result is 3201:20. I have done this with split with ":". Please suggested best way to achieve this.

DECLARE @tmpTime TABLE
(
    RowId INT IDENTITY(1, 1),
    EmployeeId INT,
    TotalWorkingTime NVARCHAR(10)
);

INSERT INTO @tmpTime
(
    EmployeeId, TotalWorkingTime
)
VALUES
(1,N'1500:30'),
(2,N'1700:50');

SELECT SUM(TotalWorkingTime) FROM @tmpTime

As you can see from @GordonLinoff's answer, your query is quite complex when using a VARCHAR to represent what is really a duration of time. If you represent your data in a more natural way, your query becomes much simpler. For example, if you store your time worked as an integer (total minutes), you can use an intermediate CTE and a couple of CROSS APPLYs to get what you need:

-- note that TotalWorkingTime is now TotalWorkingTimeMinutes
DECLARE @tmpTime TABLE
(
    RowID INT IDENTITY(1,1),
    EmployeeID INT,
    TotalWorkingTimeMinutes INT
);

-- while I'm using a calculation to show
-- how the minutes get added, this would likely
-- be  done by the application, before it gets
-- sent to the database.
INSERT INTO @tmpTime
(EmployeeID, TotalWorkingTimeMinutes)
VALUES
(1, (1500 * 60) + 30),
(2, (1700 * 60) + 50);

-- I think this intermediate CTE makes things a bit clearer.
-- but of course, you can inline it as well.
WITH SummedMinutesWorked(SummedMinutes) AS
(
    SELECT SUM(TotalWorkingTimeMinutes)
    FROM @tmpTime
)

-- you can use the CROSS APPLY to get the hours,
-- then reference those to get the "remainder minutes"
-- the SELECT has to cast your hours and minutes to a VARCHAR
-- for concatenation
SELECT CAST(H AS VARCHAR(255)) + ':' + CAST(M AS VARCHAR(255))
FROM SummedMinutesWorked
    CROSS APPLY (SELECT SummedMinutes / 60 AS H) AS HoursWorked
    CROSS APPLY (SELECT SummedMinutes - (H * 60) AS M) AS RemainderMinutes

SQL Server EOMONTH() Function By Practical Examples, ] FROM t WHERE r = 1 GROUP BY [empid], [Date] ORDER BY [empid], [Date] DROP TABLE #logs; It works by doing a self join where each side of the join is pre-filtered for the specific entry type you are looking for. SQL Server doesn't off a time type with more than 24 hours. So, don't think of what you are doing in terms of time. It is just a funky string representation of numbers. So, you can parse the value into numbers, do the summation, and then reconstruct the value:

SQL Server doesn't off a time type with more than 24 hours. So, don't think of what you are doing in terms of time. It is just a funky string representation of numbers.

So, you can parse the value into numbers, do the summation, and then reconstruct the value:

select (cast(sum(hh) + sum(mm) / 60 as varchar(255)) + ':' +
        right('00' + cast(sum(mm) % 60 as varchar(255)), 2)
       ) as hhmm
from ( VALUES (1,N'1500:30'), (2,N'1700:50') ) t(EmployeeId, TotalWorkingTime) cross apply
     (values (cast(left(TotalWorkingTime, charindex(':', TotalWorkingTime) - 1) as int),
              cast(stuff(TotalWorkingTime, 1, charindex(':', TotalWorkingTime), '') as int)
             )
     ) v(hh, mm)

SQL Script to Exclude Weekends and Holidays, How do I count the number of days in a month in SQL? Let's see how to calculate the final total using ROLLUP within SQL Query as given below: SELECT OrderID, SUM(Amount) AS Amount FROM CustomerOrders GROUP BY OrderID WITH ROLLUP Calculating Total of All Numeric columns in a row. Let's see how to calculate the total of all numeric fields within a row using SQL Query as given below:

You can try using left() and right() function for finding character before and afer ':'

select 
 concat
 (
  sum(cast(left(TotalWorkingTime,CHARINDEX(':',TotalWorkingTime)-1) as int)),
  ':',
  case when sum(cast(right(TotalWorkingTime,CHARINDEX(':',TotalWorkingTime)-3) as int))>60 then sum(cast(right(TotalWorkingTime,CHARINDEX(':',TotalWorkingTime)-3) as int))-60 else sum(cast(right(TotalWorkingTime,CHARINDEX(':',TotalWorkingTime)-3) as int)) end
 ) FROM @tmpTime

between start/end date and can subtract that from final selection. When the user updates the job status to either Started, Paused, Resumed, or Stopped, a workflow triggers and uses the current Modified value and sets it to the respective column. What I am trying to achieve is I need to calculate the total hours and minutes from when the job started until it stopped by calculating all 4 columns.

One such common user-defined function involves the ability to calculate the total number of working days and working hours within a givenĀ  Calculate A Running Total in SQL using an INNER JOIN. We first calculate the running total using the INNER JOINS. This method reveals more of the mechanics of calculating a running total than using the PARTITION. As such, it gives you another opportunity to understand INNER JOINS and apply those concepts to another use case.

Try to use something like the following. If there is a table EployeeWorkLog with the columns [EmpID], [ActionDate] and [Status]. Hide Copy Code. Total Hours working Daily I need to calculate the total hours worked, total days worked and total leaves taken Get Exact calculated total Hours and minutes in sql server

[Time] < o. [Time] ) SELECT [empid], [Date], SUM(DATEDIFF(HOUR, [in], [out])) AS [Hours] FROM t WHERE r = 1 GROUP BY [empid], [Date] ORDER BY [empid], [Date] DROP TABLE #logs; It works by doing a self join where each side of the join is pre-filtered for the specific entry type you are looking for. The SQL COUNT(), AVG() and SUM() Functions. The COUNT() function returns the number of rows that matches a specified criterion. The AVG() function returns the average value of a numeric column. The SUM() function returns the total sum of a numeric column. COUNT() Syntax

Comments
  • You can't SUM an nvarchar. The time datatype, in SQL Server, are values between 00:00:00.0000000 and 23:59:59.9999999 (although leap seconds do exist too); it can't have a value of 24:00+ (and certainly not 3200:20.
  • What makes the sum 3200:20 in your sample? Why drop 60 (minutes?).
  • What are you using as your presentation layer? Some applciations are more than happy to provide datetimes as a time value. For example, in SSRS (if I recall my date math correctly) the datetime 1900-01-12T17:15:19 could be represented as the value 281:15:19 using the format [hh]:mm:ss. If you're using Excel, then it would be 305:15:19 (As Excel's day 0 is 1900-01-00, rather than 1900-01-01).
  • We have this value in varchar column.
  • We will use this as out of box solution. Very good example with full of comments. Much appreciated. Thank you Zack.
  • This'll return 3200:80. There are only 60 minutes in an hour, not 100.