SQL Server 2008 和毫秒
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQL Server 中 DateTime 的精度始终为 1/300 秒 (3.33ms),因此任何不能精确整除的值都会四舍五入。
为了获得更高的精度,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.
To get additional accuracy, there is the DateTime2 data type, available in SQL Server 2008 onwards, that can be accurate to 7 decimal places.
日期时间的 MSDN 文档位于 http://msdn.microsoft.com/en- us/library/ms187819.aspx 说
在链接文档中还有一个部分“日期时间小数秒精度的舍入”部分。
datetime2
为您提供更高的准确性。The MSDN docs for datetime at http://msdn.microsoft.com/en-us/library/ms187819.aspx say
In the linked document there is also a section "Rounding of datetime Fractional Second Precision".
datetime2
gives you more accuracy.