[英]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. 我收到Clocked ON,Clocked OFF,(Date and Time)的原始交易,然后计算工作时间。
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. 注意事项:将日期和时间存储为日期时间类型,或日期类型和时间类型单独存储,还要考虑导出到Excel的任何限制。
Storing Date as DateTime or Date Type and Time as String, and writing a function to convert from string. 将日期存储为DateTime或日期类型和时间作为字符串,并编写一个函数以从字符串转换。
Any advice or pointer or "Watch out for GOTCHA's would be appreciated. 任何建议或指针或“留意GOTCHA's将不胜感激。
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
. 存储为
Datetime2
(我的首选项)或2列,一个纯Date
和一个纯Time
。 You can cast a Datetime2
column to date and time: 您可以将
Datetime2
列转换为日期和时间:
select DatePortion = cast(MyColumn as Date), TimePortion = cast(MyColumn as Time)
If timezones have to be considered use datetimeoffset(7)
如果必须考虑时区,请使用
datetimeoffset(7)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.