Click here to Skip to main content
15,892,797 members
Please Sign up or sign in to vote.
1.12/5 (3 votes)
I need to get the start time and end time for a day from dropdownlists
per day entries will go to database in HH:MM format
like 1 hr and 15 min OR 1.15

After 1 week we need to get the total time of work done in 1 week in HH:MM format.

Total Time : HH:MM
Posted
Comments
Sinisa Hajnal 14-Oct-14 4:18am    
1.15 doesn't equal 1:15 if you're trying to save hours and minutes as numbers... 15 minutes is a quarter of the hour and thus would be 1.25h.

Hi Ashok, I think you can achieve this by having 2 fields in your table as StartTime and EndTime with datatype as time(7). Then you can save your data these fields.

To fetch the total time you might do as below

SQL
SELECT CONVERT(NUMERIC(18, 2), SUM(DATEDIFF(minute,StartTime,EndTime)) / 60 + (SUM(DATEDIFF(minute,StartTime,EndTime)) % 60) / 100.0)
FROM TaskEntry --Where TaskEntry is yourtable name
 
Share this answer
 
If you save your data as datetime, you can use DateDiff(mi, start date, end date) to get the difference between start time and end time in minutes per day. Then sum that across the date range (one week) to get total number of minutes.

Finally, divide it by 60 to get number of hours. This should be integer. THen do modulo of the number and 60 to get remainder of minutes.
SQL
SELECT total_time = SUM(minutes_worked), hours = FLOOR(SUM(minutes_worked) / 60),
minutes = SUM(minutes_worked) % 60
FROM
(
SELECT work_date, minutes_worked = DATEDIFF('mi', start, stop) FROM your_table
WHERE work_date BETWEEN first_day_of_the_week AND end_date_of_the_week
)


If this helps, please take time to accept the solution. Thank you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900