简体   繁体   English

选择SQL数据类型日期时间,日期,时间

[英]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.

相关问题 如何将包含日期和时间的字符串转换为DateTime数据类型? - How to convert a string with date and time to DateTime data type? Sql数据类型日期也显示时间 - Sql data type date showing time as well DateTime没有使用“ DateTime”数据类型在PHPMyadmin数据库字段中保存正确的日期和时间 - DateTime is not saving correct Date and Time in PHPMyadmin database field with“DateTime” data type 如何使Entity Framework截断映射到SQL日期类型的DateTime值的时间部分? - How to make Entity Framework to truncate time portion of DateTime value mapped to SQL date type? 尝试将DateTime.Now插入日期/时间字段会出现“数据类型不匹配”错误 - Trying to insert DateTime.Now into Date/Time field gives “Data type mismatch” error 如何使用Entity Framework将当前日期/时间插入datetime数据类型字段? - How can I insert the current date/time into a datetime data type field using Entity Framework? SQL错误将数据类型datetime转换为smalldatetime? - SQL error converting data type datetime to smalldatetime? sql数据类型 - 日期OleDbSchema - sql data type - date OleDbSchema 使用哪种SQL Server sql数据类型以保留UTC日期时间 - Which SQL Server sql data type to use in order to preserve UTC date time 处理SQL时间数据类型 - Handling SQL Time data type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM