SQL Server DATEDIFF 准确性

发布于 2024-08-30 01:49:29 字数 799 浏览 5 评论 0原文

我必须在 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 技术交流群。

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

发布评论

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

评论(5

江湖彼岸 2024-09-06 01:49:29

如何计算 MS 差异(减去 DATEPART 时准确),然后将其添加到排除 MS 的 DATEDIFF 差异中?

SELECT DATEDIFF(MILLISECOND, CONVERT(VARCHAR, @StartDate, 120), CONVERT(VARCHAR, @EndDate, 120)) + DATEPART(MILLISECOND , @endDate) - DATEPART(MILLISECOND , @StartDate)

对于 0.003->.007 & 给出 4 7 表示 .000->.007

How about calculating the MS difference (which is accurate when you subtract DATEPARTs) then adding it to the DATEDIFF difference excluding MS?

SELECT DATEDIFF(MILLISECOND, CONVERT(VARCHAR, @StartDate, 120), CONVERT(VARCHAR, @EndDate, 120)) + DATEPART(MILLISECOND , @endDate) - DATEPART(MILLISECOND , @StartDate)

Gives 4 for .003->.007 & 7 for .000->.007

夜唯美灬不弃 2024-09-06 01:49:29

使用 DatePart 怎么样?

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),
        DatePart(millisecond, @EndDate-@StartDate),
        @StartDate, @EndDate

What about using DatePart

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),
        DatePart(millisecond, @EndDate-@StartDate),
        @StartDate, @EndDate
小忆控 2024-09-06 01:49:29

尝试这样

   SELECT DATEDIFF(millisecond, @StartDate, dateadd(day,1,@EndDate)),dateadd(day,1,@EndDate)-@StartDate, @StartDate, @EndDate

Try like this

   SELECT DATEDIFF(millisecond, @StartDate, dateadd(day,1,@EndDate)),dateadd(day,1,@EndDate)-@StartDate, @StartDate, @EndDate
凉栀 2024-09-06 01:49:29

我认为这里的问题是您的准确性要求对于日期时间数据类型来说太多了。

如果四舍五入到 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.

悟红尘 2024-09-06 01:49:29

或者您可能想要使用更精确的 DATETIME2 日期类型:

declare @StartDate datetime2
declare @EndDate datetime2

set @StartDate='2010-04-01 12:00:00.000'
set @EndDate='2010-04-01 12:00:00.007'

SELECT DATEDIFF(millisecond, @StartDate, @EndDate)

选择 7 作为结果。

查看 SQL Server 中的 DateTime2 与 DateTime

Or you may want use DATETIME2 date type which have more accuracy:

declare @StartDate datetime2
declare @EndDate datetime2

set @StartDate='2010-04-01 12:00:00.000'
set @EndDate='2010-04-01 12:00:00.007'

SELECT DATEDIFF(millisecond, @StartDate, @EndDate)

Selects 7 as result.

Look at DateTime2 vs DateTime in SQL Server

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