BFG
BFG

Reputation: 407

Choice of SQL Data Type Datetime, Date, Time

I am developing a Time and Attendance system (Calculate hours worked by employees, with many shift rules applied)

I receive raw transactions for Clocked ON, Clocked OFF, (Date and Time) and then calculate Time worked.

I am looking for advice from anyone with extensive Time Calculation experience on opinions OR lets call it "Best Practice" on storing the raw transactions and calculated time (Anyone in this field can appreciate that Time is a unique beast).

Considerations: Storing Date and Time as DateTime Type, or Date Type and Time Type Seperatly, also considering any limitations in exporting to Excel.

Storing Date as DateTime or Date Type and Time as String, and writing a function to convert from string.

Any advice or pointer or "Watch out for GOTCHA's would be appreciated.

Upvotes: 0

Views: 143

Answers (1)

Mitch Wheat
Mitch Wheat

Reputation: 300489

Don't store datetimes as string. There are very few (possibly zero) instances where this is a good design.

Either store as Datetime2 (my preference) OR 2 columns, one pure Date and one pure Time. You can cast a Datetime2 column to date and time:

select DatePortion = cast(MyColumn as Date), TimePortion = cast(MyColumn as Time)

If timezones have to be considered use datetimeoffset(7)

Upvotes: 2

Related Questions