Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct datatype to store "Worked Hours"

I am collecting the Worked Hours from users, the format will be like:

1:30
0:45
2:15
8:00
6:19

In SQL SERVER, what kinda of datatype should I use to store that value? I will need to sum this value in some point.

like image 356
Khrys Avatar asked Aug 03 '15 12:08

Khrys


2 Answers

Do not store worked hours, store worked minutes. Your values would become

Time int
---- ---
1:30 90
0:45 45
2:15 135
8:00 480
6:19 379

You will be able to convert it to display as necessary by dividing by 60 and taking the remainder.

Summing, on the other hand, will be very easy.

like image 76
Sergey Kalinichenko Avatar answered Oct 27 '22 21:10

Sergey Kalinichenko


SQL Server unfortunately doesn't have a data type that represents a time span1. I would normally suggest an int column and a bit of naming so that it's clear that it's in seconds/minutes/hours - whatever the granularity is that you require.


1It has a time data type but that represents a time of day, not a time span. It doesn't make sense to add two times of day together, and this being so, the time data type doesn't support addition.

like image 23
Damien_The_Unbeliever Avatar answered Oct 27 '22 21:10

Damien_The_Unbeliever