Doonie Darkoo
Doonie Darkoo

Reputation: 1495

Suitable datatype to store hours

I want to store hours in the database. The scenario is something like this that I have to store the minimum total hours for an employee until he's marked Early out. For example if the total hours spent in office of an employee are less than 7:30 than she/he left early. What should be the proper data type for this ?

Upvotes: 1

Views: 790

Answers (2)

MK_
MK_

Reputation: 1169

Take a look at data type time, available from SQL Server 2008. However, note that that's actually a data type referring to the part of a type such as datetime - so it explains time of day and therefore goes up to 24 hours.

If you need to calculate longevity (for example how long was someone at work) I'd always suggest having two timestamps (for example datetime2) and then working out the difference with DATEDIFF(). This way you have more flexibility in the future as well because you can dynamically choose whether you want to find a difference in seconds, minutes, hours, etc.

Upvotes: 4

RnP
RnP

Reputation: 400

Given your scenario, you likely don't need to store more than 24 hours. In that case, the best match is the time datatype.

See https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql

Upvotes: 1

Related Questions