SQL Server 2008和毫秒 [英] SQL Server 2008 and milliseconds

查看:188
本文介绍了SQL Server 2008和毫秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)


推荐答案

SQL Server中DateTime的准确性一直是到1/300秒(3.33ms),所以任何不分精度的值都会被舍入。

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保持原样

  • 998将轮到997

  • 999将最多可以达到000

为了获得更多的准确性,有SQL Server 2008以后的DateTime2数据类型可以精确到小数点后7位。

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

这篇关于SQL Server 2008和毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆