SQL Server 2008 和毫秒

发布于 2024-12-16 10:55:30 字数 349 浏览 5 评论 0原文

在 SQL Server 2008 中,为什么以下查询返回相同的值?

-- These all return 2011-01-01 23:59:59.997
SELECT CAST('2011-01-01 23:59:59.997' as datetime)
SELECT CAST('2011-01-01 23:59:59.998' as datetime)

为什么以下查询会舍入到第二天?

-- Returns 2011-01-02 00:00:00.000
SELECT CAST('2011-01-01 23:59:59.999' as datetime)

In SQL Server 2008, why do the following queries return the same value?

-- These all return 2011-01-01 23:59:59.997
SELECT CAST('2011-01-01 23:59:59.997' as datetime)
SELECT CAST('2011-01-01 23:59:59.998' as datetime)

And why does the following query round to the next day?

-- Returns 2011-01-02 00:00:00.000
SELECT CAST('2011-01-01 23:59:59.999' as datetime)

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

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

发布评论

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

评论(2

她如夕阳 2024-12-23 10:55:30

SQL Server 中 DateTime 的精度始终为 1/300 秒 (3.33ms),因此任何不能精确整除的值都会四舍五入。

  • 997 保持不变
  • 998 将四舍五入为 997
  • 999 将四舍五入为 000

为了获得更高的精度,SQL Server 2008 及更高版本中提供了 DateTime2 数据类型,它可以精确到小数点后 7 位。

The accuracy of DateTime within SQL Server has always been to 1/300s of a second (3.33ms), so any value that does not divide precisely gets rounded.

  • 997 stays as it is
  • 998 will round to 997
  • 999 will round up to 000

To get additional accuracy, there is the DateTime2 data type, available in SQL Server 2008 onwards, that can be accurate to 7 decimal places.

无所的.畏惧 2024-12-23 10:55:30

日期时间的 MSDN 文档位于 http://msdn.microsoft.com/en- us/library/ms187819.aspx

时间范围 == 00:00:00 至 23:59:59.997
精度 == 四舍五入为 0.000、0.003 或 0.007 秒的增量

在链接文档中还有一个部分“日期时间小数秒精度的舍入”部分。

datetime2 为您提供更高的准确性。

The MSDN docs for datetime at http://msdn.microsoft.com/en-us/library/ms187819.aspx say

Time range == 00:00:00 through 23:59:59.997
Accuracy == Rounded to increments of .000, .003, or .007 seconds

In the linked document there is also a section "Rounding of datetime Fractional Second Precision".

datetime2 gives you more accuracy.

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