在 SQL Server 中,将时间和日期拆分为两个单独的日期列是最佳实践吗?

发布于 2024-10-07 20:07:34 字数 323 浏览 2 评论 0原文

将日期时间拆分为两个日期时间 SQL 列是最佳实践吗?

例如,2010-12-17 01:55:00.000 放入两列中,

  • 其中一列包含日期时间 日期部分:2010-12-17 00:00:00.000
  • 包含日期时间的一列 对于时间部分: 1900-01-01 01:55:00.000

我被告知这是最佳实践,因为在某些时候 SQL 2000 不允许在日期中输入时间?甚至有数据存储标准强制执行这一点,并且一些公司已确保其所有数据都以这种方式存储以符合某些数据存储标准?

如果是这样的话,我肯定有人听说过,这听起来很熟悉吗?

Is it best practice to split a dateTime in two datetime SQL columns?

For example, 2010-12-17 01:55:00.000 is put in two colums,

  • one column containing a datetime for
    the date portion: 2010-12-17 00:00:00.000
  • one column containing a datetime
    for the time portion: 1900-01-01 01:55:00.000

I'm being told this is best practice because at some point SQL 2000 didn't allow to put time in a date? and that there are even data storage standards that enforce this and that some companies have ensure that all their data is stored in that manner to comply to some data storage standards?

If this is the case, I'm sure someone heard about it here, any of this sounds familiar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨曦÷微暖 2024-10-14 20:07:34

在 sql server 2008 中,您有日期和时间数据类型,因此这不再是问题。即使在 SQL Server 6 和 7 中,日期时间也始终允许时间,

人们将其分开的原因是因为对于 1 列中的所有内容,返回任意一天下午 3 点到 4 点之间下的所有订单的查询需要扫描,其中包含时间列可以通过查找来完成(快得多)

In sql server 2008 you have date and time data types so this becomes a non issue. datetime always allowed for time even back in sql server 6 and 7

the reason people split it up is because with everything in 1 column a query that returns all orders placed between 3 and 4 PM for any day requires a scan, with a time column this can be accomplished with a seek (much, much faster)

久夏青 2024-10-14 20:07:34

从 SQL 2005 开始,我只做一列。

如果您希望此信息可Sargable,我将使用 而是计算列。通过这种方式,您可以查询日期或时间或两者,并且您的应用程序代码仅负责维护一列。

Starting in SQL 2005 I would do only one column.

If you wanted this information to be Sargable I would use computed columns instead. This way you can query on date or time or both and your application code is only responsible for maintaining the one column.

吃不饱 2024-10-14 20:07:34

我知道这已经很旧了,但您可能想要保持独立的另一个原因是用户输入(GenEric 在评论中说这是为了时间管理)。如果您允许用户将日期/时间作为单独的字段输入,并且您希望能够在任一字段为空的情况下保存数据,那么数据库中最好有 2 个单独的可为空字段。否则,我想您要么必须求助于某些日期值等于“空”的拼凑,要么添加额外的位字段作为“无时间/无日期”标志。

I know this is old, but another reason you might want to keep separate is for user input (and GenEric said in a comment that this is for time management). If you allow users to enter date/time as separate fields, and you want to be able to save the data with either field being empty, it is nice to have 2 separate null-able fields in your database. Otherwise I guess you either have to resort to kludges where certain date values equal "empty" or add extra bit fields as "no time / no date" flags.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文