Skip to content
Advertisement

Adding nvarchar values as Time using SUM function in SQL Server

I am still a rookie in SQL Server so I can’t quite figure this one out.

I have a column that is filled with nvarchar values that I need to sum as durationOfWork, so it needs to do the sum function for time values.

The columns are like this:

I need to return for each Person the sum of their durations of work.

Any help would be great.

Advertisement

Answer

What a pain. I would recommend calculating the duration as minutes or decimal hours, instead of converting back to a string. The idea is to extract what you want from the string and put it in a better format:

If your values are always less than 24 hours — which they seem to be based on the format — then I would suggest storing them as time values rather than strings. That said, sum()s might exceed 24 hours, so you can’t use times for aggregated results.

EDIT:

To handle the variable minutes:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement