如果我想将一段时间保存到数据库中,我会使用什么数据类型?
我想保存游戏的持续时间。类似于 1:43:00
或 0:32:12
。
如果我使用 Microsoft SQL Server 2008 R2,我应该选择什么数据类型?
I'd like to save the duration of a game. Something like 1:43:00
or 0:32:12
.
What data type should I choose if I'm using Microsoft SQL Server 2008 R2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用整数类型,并仅存储您关心的最小精度单位的数量(在本例中为秒)。您的客户端程序代码应该担心它的格式化。因此,对于您的两个示例值,存储的数据将为
6180
和1932
。要显示值,C#(例如)将使用类似
TimeSpan.FromSeconds().ToString()
Use an integer type, and just store a quantity for the smallest unit of precision you care about (in this case, seconds). Your client program code should worry about formatting it. So for your two sample values, the stored data would be
6180
and1932
.To show the values, C# (for example) would use something like
TimeSpan.FromSeconds().ToString()
要么:
TIME
数据类型...或将开始时间和结束时间存储为
smalldatetime
我想说将 2 条记录存储为
smalldatetime
,这样您就可以选择超过 24 小时的时间,而无需更改数据结构。Either:
TIME
datatype...or store the start and end times as
smalldatetime
I would say store 2 records as
smalldatetime
so you have the option of going longer than 24 hours without needing to change your data structure.我只会存储秒数。通常存储两个日期就足够了,但我认为这将是您在游戏本身或每个会话中跟踪的内容。
I would just store the number of seconds. Normally storing two dates would suffice, but I would think that would be something you track in the game itself or per session.
可能更好地存储游戏的开始和结束时间,并根据需要动态计算持续时间。这样,您可以更改开始和/或结束时间,而无需显式更新持续时间。
至于你的问题,我将使用 int 数据类型并以分钟为单位存储持续时间。通过以分钟为单位存储,您可以轻松地将其转换为小时。 DATEDIFF 函数已经返回 INT,因此您不需要执行任何额外的操作。
Might be better to store the start and end time of the game, and calculate the duration on the fly as needed. That way, you can change the start and/or end time without having to explicitly update the duration.
As for your question, I would use an int data type and store the duration in minutes. By storing it in minutes, you can easily convert it to hours. The DATEDIFF function already returns an INT, so you wouldn't need to do any additional manipulation.