SQL Server DATEDIFF 准确性
我必须在 mssql 数据库中存储一些间隔。我知道日期时间的准确性约为。 3.3ms(只能结束0、3、7)。 但是当我计算日期时间之间的间隔时,我发现结果只能以 0、3 和 6 结尾。因此,我总结的间隔越多,精度就越差。是否有可能以毫秒为单位获得准确的 DATEDIFF ?
declare @StartDate datetime
declare @EndDate datetime
set @StartDate='2010-04-01 12:00:00.000'
set @EndDate='2010-04-01 12:00:00.007'
SELECT DATEDIFF(millisecond, @StartDate, @EndDate),@EndDate-@StartDate, @StartDate, @EndDate
我希望看到 7 ad 而不是 6。(并且应该尽可能快)
** 更新 **
我可以看到 DATEDIFF 值不仅以 0、3、6 结尾,还以 4、7 结尾(可能还有其他值)好吧)但问题是它们仍然不准确。亚历克斯建议的解决方案正在发挥作用。如果您不想记住正确的日期时间格式,也可以实现相同的效果:
SELECT DATEDIFF(SECOND, @StartDate, @EndDate)*1000 + DATEPART(MILLISECOND , @EndDate) - DATEPART(MILLISECOND , @StartDate)
我仍然想知道为什么 DATEDIFF(millisecond, @StartDate, @EndDate)
不准确?
I have to store some intervals in mssql db. I'm aware that the datetime's accuracy is approx. 3.3ms (can only end 0, 3 and 7).
But when I calculate intervals between datetimes I see that the result can only end with 0, 3 and 6. So the more intervals I sum up the more precision I loose. Is it possible to get an accurate DATEDIFF in milliseconds ?
declare @StartDate datetime
declare @EndDate datetime
set @StartDate='2010-04-01 12:00:00.000'
set @EndDate='2010-04-01 12:00:00.007'
SELECT DATEDIFF(millisecond, @StartDate, @EndDate),@EndDate-@StartDate, @StartDate, @EndDate
I would like to see 7 ad not 6. (And it should be as fast as possible)
** update **
I can see DATEDIFF values ending not just with 0, 3, 6 but also 4, 7 (there might be others as well) but the thing is that they are still inaccurate. The solution suggested by Alex is working. The same can be achived if you don't wan't to remember the correct datetime format with:
SELECT DATEDIFF(SECOND, @StartDate, @EndDate)*1000 + DATEPART(MILLISECOND , @EndDate) - DATEPART(MILLISECOND , @StartDate)
I still wonder why DATEDIFF(millisecond, @StartDate, @EndDate)
is inaccurate ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如何计算 MS 差异(减去
DATEPART
时准确),然后将其添加到排除 MS 的DATEDIFF
差异中?对于 0.003->.007 & 给出 4 7 表示 .000->.007
How about calculating the MS difference (which is accurate when you subtract
DATEPART
s) then adding it to theDATEDIFF
difference excluding MS?Gives 4 for .003->.007 & 7 for .000->.007
使用
DatePart
怎么样?What about using
DatePart
尝试这样
Try like this
我认为这里的问题是您的准确性要求对于日期时间数据类型来说太多了。
如果四舍五入到 6 或 7 毫秒是一个问题,那么您将永远无法获得所需的精度。
你的间隔是连续的吗?如果是这样,您是否可以只存储一个日期,然后计算第一个开始日期和最后一个结束日期之间的毫秒数?
或者,您可以使用您的客户端语言获取间隔吗?然后将间隔存储为 int/long?您可以存储在代码中计算的开始日期和以毫秒为单位的间隔,而不是在 sql 中存储开始和结束日期。
I think the issue here is that your accuracy requirements are too much for the datetime datatype.
If rounding to 6 or 7 milliseconds is an issue then you will never get the accuracy you need.
Are the intervals you have continuous? If so, could you just store a single date and then calculate the milliseconds between the first start date and the last end date?
Alternatively, can you obtain the interval using your client language? Then store the intervals as an int/long? You could possibly store the start date and the interval in milliseconds calculated in code rather than storing a start and end date in sql.
或者您可能想要使用更精确的 DATETIME2 日期类型:
选择 7 作为结果。
查看 SQL Server 中的 DateTime2 与 DateTime
Or you may want use DATETIME2 date type which have more accuracy:
Selects 7 as result.
Look at DateTime2 vs DateTime in SQL Server